Skip to content

Instantly share code, notes, and snippets.

@adamfairholm
Last active March 24, 2016 01:41
Show Gist options
  • Save adamfairholm/af88caf5bd0e585154ed to your computer and use it in GitHub Desktop.
Save adamfairholm/af88caf5bd0e585154ed to your computer and use it in GitHub Desktop.
Laravel's where date functions.
<?php
// Laravel has some date functions in the query builder that I never really
// see mentioned. Most of the time when a user needs to do a MONTH(col) select,
// they are advised to do something like this:
$employees = Employee::where(DB::Raw('MONTH(hired_at)'), '=', 4)->get();
// However, Laravel seems to have query builder functions that take care of this.
// The above query could be rewritten as:
$employees = Employee::whereMonth('hired_at', '=', 4)->get();
// There are also functions for whereYear(), and whereDay().
// This all resolves to a dateBasedWhere() function in the query grammar
// that is defined in the main Grammar and extended for the postgres grammar.
@hengsoheak
Copy link

I used same as you why It not work
$tran = TillTransaction::where(DB::raw('DATE(open_time)'),'=','2016-03-24')->get();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment