dongqiya9552 2017-07-20 20: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 21: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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)