Search code examples
phplaravelphp-carbon

Laravel date format in where condition


I have this code

use Carbon;
$now = Carbon::now()->toDateString(); 

So i get the date without the time

public funtion test($brandid){

     $nbofsale=DB::table('st_nsales')
                         ->where('brand_id', $brandid)
                         ->where('eodate', $now)
                         ->get();
     return $nbofsale; 
}

The eodate field in the select is datetime. I want to put it in this query as date only and not to change the field type in database. Any ideas?


Solution

  • If eodate is a timestamp, you don't need to convert it to a date string:

    $nbofsale = DB::table('st_nsales')
                  ->where('brand_id', $brandid)
                  ->whereDate('eodata', Carbon::today()->toDateString());
                  ->get();
    

    Another way to get record for today:

    $nbofsale = DB::table('st_nsales')
                  ->where('brand_id', $brandid)
                  ->where('eodata', '>', Carbon::now()->startOfDay());
                  ->get();