Egyptionl
Egyptionl

Reputation: 63

Laravel where dates by specific format

In DB Col Name('publish') varchar format d/m/Y for Example 30/1/2020 when try to get dates less than 30/1/2020 get any date less than days

for example

->where('publish','<','30/01/2020')

29/2/2020 less than 30/1/2020 so i get any date under 30 by day not month or year

Upvotes: 0

Views: 2352

Answers (2)

Sehdev
Sehdev

Reputation: 5682

You can use DB::raw and str_to_date for the same:

 ->where(DB::raw('str_to_date(publish, "%d/%m/%Y")'),'<','30/01/2020' )

str_to_date will convert your varchar field to datetime field.

MySQL -> Date and Time Functions-> STR_TO_DATE

Upvotes: 0

Moshiur
Moshiur

Reputation: 685

on your Eloquent model define an accessor to retrive publish as carbon object

public function getPublishAttribute($date)
{
    return Carbon::parse($date)->format('d/m/Y');
}

now on your controller compare both date as below

->where('publish','<',Carbon::parse('30/01/2020'))

hope it helps!

Upvotes: 1

Related Questions