
wheredate in laravel not working
There could be any issue in script while comparing with column's value that's why wheredate method is not working. WhereDate method is used to compare a column's value against a date in laravel
-
Laravel whereDate() method on created_at field
$users = User::whereDate('created_at', '=', date('Y-m-d'))->get(); return $users; //Or $users = User::whereDate('created_at', '=', '2021-8-31')->get(); return $users;
0Output:
[{"id":16,"name":"Kayla","email":"[email protected]","email_verified_at":null,
"created_at":"2021-08-31T08:24:43.000000Z","updated_at":"2021-08-31T08:24:43.000000Z"}]This code snippet will help you to get the records from users table on the base of current (today) date. You can pass date "2021-8-31" in ('Y-M-D') format as third argument to whereDate() method to get the record of specific date from table.
-
Laravel whereDate() method on created_at field with dynamic value
//2021-08-02 $users = User::whereDate('created_at', '=', User::find(1)->created_at->toDateString())->get(); return $users; //2021-08-02 $users = User::whereDate('created_at', '=', User::find(1)->created_at->format('Y-m-d'))->get(); return $users; //Output : [{"id":16,"name":"Kayla","email":"[email protected]","email_verified_at":null,"created_at":"2021-08-02T08:24:43.000000Z","updated_at":"2021-08-31T08:24:43.000000Z"}]
0You can pass dynamic value to whereDate method using created_at() and toDateString() method on create_at attribute. The method format() and toDataString() convert the timestamp (date and time) value into date format.
Random Code Snippet Queries: Laravel
- Create project factory and seed data in laravel
- How to check data inserted or deleted in pivot after toggle method
- Property [user] does not exist on this collection instance
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- How to display a specific word from a string in laravel
- Class 'App\Rules\Hash' not found in Laravel
- Shorter syntax for whereHas with call back function in laravel
- Page loader in laravel
- Use withCount() to Calculate Child Relationship Records
- Call to undefined method App\Models\User::follow()
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Laravel hasmany select not working
- Return redirect laravel not working
- How to get CSRF token in laravel controller
- How to get records in random order in laravel
- How to get date from created_at field in laravel
- Get previous date data in laravel
- Target class [HomeController] does not exist
- Laravel create multiple records in Pivot table
- Laravel URL validation not working
- The use statement with non-compound name 'DB' has no effect
- Get laravel version
- First and last item of the array using foreach iteration in laravel blade
- Laravel change date format
- Post model with title and body in laravel 8