Home » Php » php – Laravel where dates by specific format

php – Laravel where dates by specific format

Posted by: admin February 25, 2020 Leave a comment


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


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

How to&Answers:

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


hope it helps!


Write your query like

use whereDate

$date = Model::whereDate('publish', '>', '30/01/2020')->get();


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