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 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页