我正在努力寻找满足大多数条件的原料。
我有一个名为SLA
的模型;我想找一个SLA
。
我有2 SLAs
SLA1 :
type = 1
SLA2
type = 1
department = 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());
}
答案 0 :(得分:0)
我希望你不能只使用构建器,你也必须使用收集方法
这是我的答案,请查看
$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];
}
}