Laravel-日期范围搜索过滤器不起作用

时间:2019-06-05 03:07:23

标签: laravel eloquent

我正在尝试执行不同的搜索过滤器:用户名,电子邮件和日期范围。除日期搜索外,所有其他搜索过滤器均有效。

控制器

public function revenueDetail(Request $request, $plan = null)
{
    $data['title'] = 'Revenue Detail Report';
    $revenuedetails = DB::table("users")
        ->select("users.username", "users.email",DB::raw("DATE(users.created_at) as subscription_date"))
        ->where('users.plan', self::$type[$plan] ?? null); // ?? will escape exception if $plan is null
        $render=[];
        if(isset($request->start_date) && isset($request->end_date))
        {
            $revenuedetails=$revenuedetails->whereBetween('created_at',[$request->start_date,$request->end_date]);
            $render['start_date']=$request->start_date;
            $render['end_date']=$request->end_date;
        }elseif(isset($request->start_date))
        {
            $revenuedetails=$revenuedetails->where('created_at',$request->start_date);
            $render['start_date']=$request->start_date;
        }        
        if(isset($request->username))
        {
            $revenuedetails=$revenuedetails->where('username','like','%'.$request->username.'%');
            $render['username']=$request->username;
        }
        if(isset($request->email))
        {
            $revenuedetails=$revenuedetails->where('email','like','%'.$request->email.'%');
            $render['email']=$request->email;
        }        
        $revenuedetails= $revenuedetails->orderBy('subscription_date','DESC');
        $revenuedetails= $revenuedetails->paginate(15);
        $revenuedetails= $revenuedetails->appends($render);
        $data['revenuedetails'] = $revenuedetails;
    return view('revenue.revenueDetail',$data);
}   

查看

    <div class="row" style="margin-bottom: 10px">
        {{ Form::model(request(),['method'=>'get']) }}
        <div class="col-sm-2">
             {{ Form::text('username',null,['class'=>'form-control','placeholder'=>'Username']) }}
        </div>
        <div class="col-sm-2">
            {{ Form::text('email',null,['class'=>'form-control','placeholder'=>'Email']) }}
        </div>
        <div class="col-sm-3">
            {{ Form::date('start_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
        </div>  
        <div class="col-sm-3">
            {{ Form::date('end_date',null,['class'=>'form-control','placeholder'=>'Date']) }}
        </div>        
        <div class="col-xs-2">
            {{ Form::submit('Search',['class'=>'btn btn-warning']) }}
            <a href="{{ route('gamesexport') }}" class="btn btn-primary"><i class="fa fa-file-excel-o"></i> Excel</a>
        </div>
        {{ Form::close() }}
    </div>


    <tbody>
        @foreach($revenuedetails as $key => $revenuedetail)
            <tr>
                <td>{{ ++$key }}</td>
                <td>{{ $revenuedetail->username }}</td>
                <td>{{ $revenuedetail->email }}</td>
                <td>{{ $revenuedetail->subscription_date }}</td>
            </tr>

        @endforeach
            <tr>
            <td colspan="8">
                {{ $revenuedetails->links() }}
            </td>
            </tr>         
    </tbody>

当我单击搜索按钮时,我希望看到显示的结果。除日期搜索过滤器外,所有其他搜索过滤器均正常工作。它不会显示任何结果。

  1. 我如何使其工作

  2. 如何将日期格式化为dd / mm / yyyy

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您没有将$ request-> start_date用作Y-m-d H:i:s格式,则可能需要将其转换为该格式,并且如果您只是传递日期(即Ymd)并希望在您的位置进行比较查询应该是

$start_date = Carbon\Carbon::parse($request->start_date)->toDateTimeString()
$end_date = Carbon\Carbon::parse($request->end_date)->toDateTimeString()
$revenuedetails=$revenuedetails->whereBetween('created_at',[$start_date, $end_date]);