我试图找到一个满足大多数条件的原始文件。
我有一个名为SLA
的模型;我试图找到一个SLA
。
我有2个SLAs
SLA1 :type = 1
SLA2type = 1department = 2
当我打电话时:
sla($type=1,$user_id=“”,$dept=“”,$source=“”,$priority=“”);
它返回
SLA1
。当我打电话时:sla($type=1,$user_id=“”,$dept=1,$source=“”,$priority=“”);
它返回
SLA1
而不是SLA2
。这是我的代码:function sla($type = "", $userid = "", $department = "", $source = "", $priority = "") {
$sla = \App\Model\helpdesk\Manage\Sla\Sla_plan::
where(function($query)use($type, $department, $source) {
$query->where(function($q) use($department) {
$q->whereRaw("find_in_set($department,apply_sla_depertment)");
})
->where(function($q) use($type) {
$q->whereRaw("find_in_set($type,apply_sla_tickettype)");
})
->where(function($q) use($source) {
$q->whereRaw("find_in_set($source,apply_sla_ticketsource)");
});
})
->orWhere(function($query)use($type, $department, $source) {
$query->orWhere(function($q) use($department) {
$q->whereRaw("find_in_set($department,apply_sla_depertment)");
})
->orWhere(function($q) use($type) {
$q->whereRaw("find_in_set($type,apply_sla_tickettype)");
})
->orWhere(function($q) use($source) {
$q->whereRaw("find_in_set($source,apply_sla_ticketsource)");
});
});
dd($sla->first());
}
最佳答案
我希望您不能只使用builder,还必须使用收集方法
这是我的答案,请检查此
$sla = \App\Model\helpdesk\Manage\Sla\Sla_plan::
where('status',1)
->where(function($query)use($type, $department, $source, $company) {
$query
->where(function($query)use($type, $department, $source, $company) {
$query
->when($type, function($query)use($type) {
$query->orWhereRaw("find_in_set($type,apply_sla_tickettype)");
})
->when($department, function($query)use($department) {
$query->orWhereRaw("find_in_set($department,apply_sla_depertment)");
})
->when($source, function($query)use($source) {
$query->orWhereRaw("find_in_set($source,apply_sla_ticketsource)");
})
});
});
$all = $sla->get();
if ($all->count() > 0) {
$collection = $all->mapWithKeys(function($value)use($type, $department, $source,$company) {
$array = [$value->id => 0];
if (is_array($value->apply_sla_tickettype) && in_array($type, $value->apply_sla_tickettype)) {
$array[$value->id] ++;
}
if (is_array($value->apply_sla_depertment) && in_array($department, $value->apply_sla_depertment)) {
$array[$value->id] ++;
}
if (is_array($value->apply_sla_ticketsource) && in_array($source, $value->apply_sla_ticketsource)) {
$array[$value->id] ++;
}
return $array;
});
$array = $collection->toArray();
if ($array) {
$maxs = array_keys($array, max($array));
return $maxs[0];
}
}