搜索查询条件不起作用-Laravel 5.7

时间:2018-11-10 13:36:42

标签: php laravel

我认为:

<div class="container">
<div class="row">
    <div class="col-lg-12 col-md-12">
        <form class="search-form search-form-basic" action="/candidates/index" method="post">
        {{ csrf_field() }}
        <div class="form-row">              
            <div class="col-md-4 form-group">
                <label for="search_email">First name:</label>
                <input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value="{{ Session::get('inputs')['search_first_name'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_last_name">Last name:</label>
                <input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value="{{ Session::get('inputs')['search_last_name'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_round_number">Round Number:</label>
                <input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value="{{ Session::get('inputs')['search_round_number'] }}" @endif>
            </div>  
            <div class="col-md-4 form-group">
                <label for="search_location">location:</label>
                <input type="text" name="search_location" id="search_location" class="form-control" 
                 @if(isset(Session::get('inputs')['search_location'])) value="{{ Session::get('inputs')['search_location'] }}" @endif>
            </div> 
            <select name="options" class="col-md-4 form-group">
             <option value="">--- Select From ---</option>
              <option  value="birth_date">Birth Date</option>
              <option  value="CV_grade_date">CV Grade Date</option>
              <option  value="source">Source</option>
              <option  value="recommendation">Recommended</option>                
              <option  value="created_at">Applied</option>
            </select>
            <div class="col-md-4 form-group">
                <label for="search_from_date">From:</label>
                <input type="date" name="search_from_date" id="search_from_date" class="form-control" 
                 @if(isset(Session::get('inputs')['search_from_date'])) value="{{ Session::get('inputs')['search_from_date'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_to_date">To:</label>
                <input type="date" name="search_to_date" id="search_to_date" class="form-control" 
                 @if(isset(Session::get('inputs')['search_to_date'])) value="{{ Session::get('inputs')['search_to_date'] }}" @endif>
            </div>  
        </div>
        <div class="form-row">
            <div class="col-md-12 col-lg-12">
                <button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
                <a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
            </div>
        </div>
    </form>
</div>

我的控制器:

    public function index(Request $request) {

    if($request->isMethod('post')){
        $search_first_name =    $request->search_first_name;
        $search_last_name =     $request->search_last_name;
        $search_email =         $request->search_email;
        $search_round_number =  $request->search_round_number;
        $search_location =      $request->search_location;
        $search_from_date =      $request->search_from_date;
        $search_to_date =      $request->search_to_date;

        $options = Input::get('options');

        $recOrSource = null;

        if($options == 'recommendation' || $options == 'source')   {
                $recOrSource = Input::get('options');
                $options == null;  

        }

        $candidate = DB::table('candidates')
                    ->when($search_first_name, function ($query) use ($search_first_name) {
                        return $query->where('first_name', 'like', '%' . $search_first_name . '%');
                    })
                    ->when($search_last_name, function ($query) use ($search_last_name) {
                        return $query->where('last_name', 'like', '%' . $search_last_name . '%');
                    })
                    ->when($search_email, function ($query) use ($search_email) {
                        return $query->where('email', $search_email);
                    })
                    ->when($search_round_number, function ($query) use ($search_round_number) {
                        return $query->where('round_number', $search_round_number);
                    })
                    ->when($search_location, function ($query) use ($search_location) {
                       return $query->where('location', $search_location);
                    })                           
                    ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date) {
                       return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date));
                    })                       
                    ->when($options, function ($query) use ($options,$search_from_date,$search_to_date  ) {
                       return $query->whereBetween($options, array($search_from_date, $search_to_date));
                    })                                                                                      
                    ->orderBy('first_name', 'asc')
                    ->orderBy('last_name', 'asc')
                    ->get();                     

                    Session::flash('inputs', [
                        'search_first_name' => $search_first_name,
                        'search_last_name' => $search_last_name,
                        'search_email' => $search_email,
                        'search_round_number' => $search_round_number,
                        'search_from_date' => $search_from_date,
                        'search_to_date' => $search_to_date,
                        'search_location' => $search_location

                        ]);
                }else{
                     Session::forget('inputs');


        $candidate = Candidate::orderBy('first_name', 'asc')
                        ->orderBy('last_name', 'asc')
                        ->get();
                    }

        return view('/candidates/index', [
            'candidate' => $candidate
        ]);

当我选择“源”作为选项之一,并输入“从-到”日期时,我收到此错误:

  

找不到列:1054“ where子句”中的未知列“ 1”(SQL:从candidates中选择*,其中1为空,created_at在2015-8-8之间2019-1-1和source在2015-8-8和2019-1-1之间按first_name asc,last_name asc排序)

但是,dd($ recOrSource)返回正确的值;我看不到“ 1”的来源,以及为什么要运行此部分

                        ->when($options, function ($query) use ($options,$search_from_date,$search_to_date  ) {
                       return $query->whereBetween($options, array($search_from_date, $search_to_date));
                    })     

当我有

            if($options == 'recommendation' || $options == 'source')   {
                $recOrSource = Input::get('options');
                $options == null;  

        }

0 个答案:

没有答案