gpt4 book ai didi

php - 如何修改已执行的 Laravel 查询生成器

转载 作者:行者123 更新时间:2023-11-30 21:56:12 24 4
gpt4 key购买 nike

我正在尝试找到满足大多数条件的原始数据。

我有一个名为 SLA 的模型;我正在尝试找到一个 SLA

我有 2 个SLA

  • SLA1:
    类型 = 1

  • SLA2
    类型 = 1
    部门 = 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,你还得用collection方法

这是我的回答,请检查一下

$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];
}
}

关于php - 如何修改已执行的 Laravel 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com