2017-07-15 123 views
1

我正在尝试查找满足大多数条件的原始数据。如何修改执行的Laravel查询生成器

我有一个模型叫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

我希望你不能只是做建设者,你必须使用收集方法太

这就是我的回答,请检查该

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