Laravel wherebetween之间不起作用。表明?

时间:2019-06-06 13:21:53

标签: mysql laravel

我正在尝试从leads表中获取上周数据,但是当我使用带有两个日期的whereBetween时,找不到数据,如果使用toSql(),它会显示吗? ?

查询是

    select `lead_type`, `email`, `first_name`, `last_name`, `phone_number`, `job_title`, `company_name`, `website`, `time_note`, `update_date` from `leads` where `created_at` between ? and ? 

但应该是

    select `lead_type`, `email`, `first_name`, `last_name`, `phone_number`, `job_title`, `company_name`, `website`, `time_note`, `update_date` from `leads` where `created_at` between 2019-06-06 and 2019-05-30

这是Laravel代码

    $current=  Carbon::today();
    $start_date = Carbon::today()->toDateString();
    $end_date = $current->subDays(7)->toDateString();
    $leads = Leads::select($excel_head)->whereBetween('created_at', [$end_date,$start_date ])->toSql();
    dd($leads);

2 个答案:

答案 0 :(得分:1)

不要在查询中包含?,因为它是已准备好的语句。

尝试运行此

$current=  Carbon::today();

$start_date = Carbon::today()->toDateString();
$end_date = $current->subDays(7)->toDateString();

$leads = Leads::select($excel_head)->whereBetween('date(created_at)', array($start_date, $end_date))->get();

dd($leads);

您必须将created_at转换为date字段,然后进行比较。

答案 1 :(得分:0)

您将开始日期和结束日期倒退了。

whereBetween('created_at', [$end_date, $start_date])

应该成为

whereBetween('created_at', [$start_date, $end_date])

否则,您的查询基本上是在这样做:

created_at>='2019-06-06' AND created_at<='2019-05-30'

这不可能是真的。