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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?