Laravel Multi属性搜索未按我的意愿进行过滤

时间:2017-06-21 07:53:21

标签: php laravel search laravel-5

我想要的是当我选择开始日期和结束日期然后按搜索按钮。那么数据需要被过滤。

这是界面。

enter image description here 以下是相关的观点。

<div class="col-md-12">
  <div class="jumbotron">
    <div class="col-md-offset-3">
      <div class="form-group">
        <form action="AdminpredictionSearch" method="post" class="form-inline">
          <div name="institute" id="institute">
            <div class="form-group">
              <label>Starting Date:</label>
              <input class="form-control" name="start_date" type="date" value="" id="example-date-input">
            </div>
            <div class="form-group">
              <label>End Date:</label>
              <input class="form-control" name="end_date" type="date" value="" id="example-date-input">
            </div>
            <input type="hidden" value="{{ csrf_token() }}" name="_token" />
            <input type="submit" name="submit" value="Search">
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

这是控制器。

public function admin_predictions(Request $request)
{    
    $query = $request->search;
    $queryType = $request->institute; // 'id' or 'name'
    $items = DB::table('registerdetails');        

    if($queryType == 'start_date'){
        $items = $items->where('start_date', 'LIKE',"%$query%");
    }
    if($queryType == 'end_date'){
        $items = $items->where('end_date', 'LIKE',"%$query%");
    }
    $items = $items->get();

    return view('registeredusers.adminpredictions')->with('items',$items);
}

这是路线

Route::post('AdminpredictionSearch','UserRegisterController@admin_predictions');

任何人都可以建议我为什么不进行过滤。

2 个答案:

答案 0 :(得分:1)

  public function admin_predictions(Request $request)
{
    $start_date = Input::get('start_date');
    $end_date = Input::get('end_date');
    $items = DB::table('registerdetails');
    if ($start_date) {
        $items = $items->where('start_date', '=', $start_date);

    } elseif ($end_date) {
        $items = $items->where('end_date', '=', $end_date);
    } elseif ($start_date && $end_date) {
        $items = $items->where('end_date', '=', $end_date)->where('start_date', '=', $start_date);
    } else {

        $items = $items;
    }
    $items = $items->get();
    return view('registeredusers.adminsearch')->with('items', $items);
}

将控制器更改为此

答案 1 :(得分:0)

    // here User is your model in controller
    //namespace App\Http\Controllers;
    //Use App\User;
    $search = 'foo';
    $user = User::where('name','LIKE',"%{$search}%")->get();
    print_r($user);