Home > Net >  Select Laravel eloquent data where created_at matched from a date array
Select Laravel eloquent data where created_at matched from a date array

Time:08-16

I have a CLICK model that contains click information of users.

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.

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.

Note that: it is not ->whereBetween(), I want to match specific date contains in a $date array.

CodePudding user response:

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) . "')");

CodePudding user response:

Assuming your created_at is a timestamp:

You can chain orWhereRaw()s with 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'")
    ...
  • Related