简体   繁体   English

Select Laravel eloquent 数据,其中 created_at 与日期数组匹配

[英]Select Laravel eloquent data where created_at matched from a date array

I have a CLICK model that contains click information of users.我有一个包含用户点击信息的CLICK model。

I want to get click data where specific date matched.我想获取特定日期匹配的点击数据。

Like,喜欢,

$date = ['09/08/2022', '09/16/2022', '03/08/2022']

I want that clicks which created_at are match with one element of $date array.我希望created_at的点击与$date数组的一个元素匹配。

Laravel has ->whereIn() eloquent function to do almost same task, but in my case created_at and $date array element does not has same date format. Laravel 有->whereIn() eloquent function 做几乎相同的任务,但在我的情况下created_at$date数组元素没有相同的日期格式。

Note that: it is not ->whereBetween() , I want to match specific date contains in a $date array.请注意:它不是->whereBetween() ,我想匹配$date数组中包含的特定日期。

Try this one:)试试这个:)

 $arr = ['07/07/2022', '07/08/2022', '07/09/2022'];
 $query->whereRaw("DATE_FORMAT(created_at, '%m/%d/%Y') IN('" . join("','", $arr) . "')");

Assuming your created_at is a timestamp:假设您的created_at是时间戳:

You can chain orWhereRaw() s with LEFT() :您可以将orWhereRaw()LEFT()链接起来:

$myQuery
    ->whereRaw("LEFT(created_at, 10) = '2022-09-08'")
    ->orWhereRaw("LEFT(created_at, 10) = '2022-09-16'")
    ->orWhereRaw("LEFT(created_at, 10) = '2022-08-03'")
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM