dongqiya9552 2017-07-20 12:25
浏览 16
已采纳

在laravel中从一系列日期中获取行

I am trying to fetch rows from database by applying a range of two dates. starting and ending

This below is a test data.

Collection {#258 ▼
  #items: array:4 [▼
    0 => Customer_history {#237 ▶}
    1 => Customer_history {#260 ▶}
    2 => Customer_history {#261 ▶}
    3 => Customer_history {#262 ▼
      #attributes: array:12 [▼
        "ch_id" => 1
        "customer_id" => 1
        "invoice_id" => 12
        "created_at" => "2017-07-17 22:57:55"
        "updated_at" => "2017-07-17 22:57:55"
        "branch_id" => 1
        "salesman_id" => 1
        "remarks" => ""
        "invoice_no" => ""
        "advance" => ""
        "is_delivered" => 1
        "is_paid" => 1
      ]
    }
  ]
}

As you see there is one two that has date 2017-07-17 Now when I applied query with two dates created

$from = date_create("2017-07-17");
$till = date_create("2017-07-17");

$customer_histories = Customer_history::leftJoin('invoices AS i', 'i.invoice_id', 'customer_histories.invoice_id')
        ->where('is_paid','=',1)
        ->whereBetween('customer_histories.created_at', [$from,$till])
        ->get();

This doesn't return any rows and it should have return 1 row! I guess I am missing out of something. Can anyone help me out what should be omitted or amended to make it work?

展开全部

  • 写回答

1条回答 默认 最新

  • doutu7123 2017-07-20 13:00
    关注

    Try this. remove these lines

    $from = date_create("2017-07-17");
    $till = date_create("2017-07-17");
    

    And put this query

    $customer_histories = Customer_history::leftJoin('invoices AS i', 'i.invoice_id', 'customer_histories.invoice_id')
        ->where('is_paid','=',1)
        ->whereRaw('DATE_FORMAT(customer_histories.created_at, "%Y-%m-%d") BETWEEN ? AND ?', [ $from, $till])
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部