adding today() filter on the ORM Elquent model #47420
Unanswered
mohammad-azzam-dev
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I propose a method called today(dateField: 'date_field') on the eloquent ORM. This method allows querying records by filtering data that correspond to the current day. It accepts an optional parameter $dateField, which specifies the date field to be used for filtering. If $dateField is not provided, the method will default to using the created_at field. Alternatively, you can specify a different default date field by defining a private property in the model class.
I have implemented it by a scopMethod, hope to accept this request and allow me to add it to the Laravel framework and code it :D
To implement the code in the Builder class it would be something like this
public function today(string $field='created_at')
{
$now = \Carbon::now();
return $this->whereBetween($field,[$now->startOfDay(), $now->endOfDay()]);
}
Beta Was this translation helpful? Give feedback.
All reactions