Laravel日期搜索功能

时间:2018-06-08 05:51:15

标签: php laravel

如何添加DATE_FROM和DATE_TO等日期搜索?我已经使用搜索框和下拉过滤器的现有控制器到我的刀片模板,现在我如何添加日期范围过滤器。以及如何将其添加到我的刀片日期选择器

注意: 在我的数据库中我只有默认的created_at列

控制器:

public function index(Request $request)
{

    $search=$request->input('search'); 

    if(request()->has('lead_status')){
        $leads=Lead::where('lead_status', request('lead_status'))
        ->paginate(5)
        ->appends('lead_status',request('lead_status'));
    }
    else{

        $leads=Lead::orderBy('created_at','desc')->search($search)->paginate(5);

    }

    return  view ('leads.index')->with('leads',$leads);

}

查看日期选择器

 <div class="col-sm-4 form-group">
    <label>FROM</label>
    <input type="date"  name="" value="" class="form-control">
                               
</div>
<div class="col-sm-4 form-group">
    <label>TO</label>
    <input type="date"  name="" value="" class="form-control">
                               
</div>

2 个答案:

答案 0 :(得分:0)

您可以按日期搜索日期 并且可以在Laravel

更多
Lead::whereDate('created_at', '=', date('Y-m-d'))->get();

WhereBetween

Lead::whereBetween('created_at', [date('Y-m-d', strtotime($input['from'])), date('Y-m-d', strtotime($input['to']))])->get();

你的解决方案

$leads = Lead::query();
    if($request()->has('lead_status')){
        $leads = $leads->where('lead_status', $request('lead_status'));
    }
    if($request()->has('from') && $request()->has('to')){
    {
        $from_date = date('Y-m-d', strtotime($request()->has('from')));
        $to_date = date('Y-m-d', strtotime($request()->has('to')));
        $leads = $leads->whereBetween('created_at', [$from_date, $to_date]);
    }

    $leads = $leads->orderBy('created_at','desc')->paginate(5);

答案 1 :(得分:0)

在您的控制器中访问请求输入参数,如下所示:     使用Illuminate \ Http \ Request;

public function index(Request $request)
{
    $search= $request->input('date');
    $leads=Lead::where('lead_status''=', $search)->get();

    return  view ('leads.index')->with('leads',$leads);
}

ROUTE Route::get('/leads/{date}', 'YourControllerName@index');