doue1925 2017-07-15 13:20
浏览 80
已采纳

如何修改已执行的Laravel查询构建器

I am trying to find a raw which satisfies, most conditions.

I have a model called SLA; I am trying to find one SLA.

I have 2 SLAs

  • SLA1 : type = 1

  • SLA2 type = 1 department = 2

When I call:

sla($type=1,$user_id=“”,$dept=“”,$source=“”,$priority=“”);

it returns SLA1. When I call:

sla($type=1,$user_id=“”,$dept=1,$source=“”,$priority=“”);

it returns SLA1 instead of SLA2. Here's my code:

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());
}
  • 写回答

1条回答 默认 最新

  • dtbl1231 2017-07-18 07:38
    关注

    I hope you can't do just with builder, You have to use collection methods too

    this is my answer, please check this

    $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];
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线