我尝试按以下代码进行复杂查询。 我的目标是通过
创建三个不同的查询请求语句我试图制作但我不知道如何使用when
方法。
我认为if - else
方法应该是解决方案,而不是 public function jobSearch(Request $request)
{
$query = Job::onlyTrashed()
->where('is_trash', 1)
->where( function ($queryByDate) use($request)
{
$queryByDate->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orwhere('user_name', 'like', '%'.$request->name.'%');
})
->orderBy('deleted_at', 'desc');
$job_ids = $query->pluck('id')->all();
// $job_ids = $query->pluck('id');
$jobs = $query->paginate(15);
if (empty($job_ids)){
Flash::error('Search result not found.');
return redirect(route('retrievejobs.index'));
}
return view('retrievejobs.index', ['jobs' => $jobs]);
}
语句。
我怎么能这样做?
控制器
<div style="overflow-x: auto; display: inline-flex;">
{!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/stopjobs/jobsearch'))) !!}
{{ Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) }}
{{ Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) }}
{!! Form::text('name', null, ['placeholder' => 'Bruker eller Kunde']) !!}
{!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
{!! Form::close() !!}
查看
{{1}}
答案 0 :(得分:0)
您需要创建多个if语句并根据if语句链接查询。
示例:
$query = Job::onlyTrashed()
->where('is_trash', 1)
->where( function ($queryByDate) use($request)
{
if($request->something){
$queryByDate->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orwhere('user_name', 'like', '%'.$request->name.'%');
}else if($request->something else){
$queryByDate->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->orwhere('user_name', 'like', '%'.$request->name.'%');
}
})
->orderBy('deleted_at', 'desc');
答案 1 :(得分:0)
请尝试以下操作。
您需要根据您的要求设置$matchCustomerName
和$matchUsername
变量。
public function jobSearch(Request $request)
{
$query = Job::onlyTrashed()
->where('is_trash', 1)
->when($request->input('datepicker_from'), function($query) use($request) {
$query->whereBetween('created_at', [
Carbon::parse($request->input('datepicker_from'))->startOfDay(),
Carbon::parse($request->input('datepicker_to'))->endOfDay()
]);
})->when($request->customer_name, function($query) use($request) {
$matchCustomerName = true;
$query->where('customer_name',
'like',
"%{$request->customer_name}%",
$matchCustomerName ? 'and' : 'or');
})->when($request->name, function($query) use($request) {
$matchUsername = true;
$query->where('user_name',
'like',
"%{$request->name}%",
$matchUsername ? 'and' : 'or');
})->orderBy('deleted_at', 'desc');
$jobs = $query->paginate(15);
if ($jobs->isEmpty()){
Flash::error('Search result not found.');
return redirect(route('retrievejobs.index'));
}
return view('retrievejobs.index', ['jobs' => $jobs]);
}