使用Laravel Query Builder的查询

时间:2018-04-06 08:55:41

标签: laravel laravel-query-builder

我尝试按以下代码进行复杂查询。 我的目标是通过

创建三个不同的查询请求语句

polyfill

  1. 仅限每一个(日期或user_name或customer_name)
  2. 日期和用户名
  3. 日期和客户名称。
  4. 我试图制作但我不知道如何使用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']) }}
            &nbsp;
        {{ Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) }} 
    &nbsp;
        {!! Form::text('name', null, ['placeholder' => 'Bruker eller Kunde']) !!}
        {!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!} 
        {!! Form::close() !!}
    

    查看

    {{1}}

2 个答案:

答案 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]); 
}