Laravel delete all rows older than 30 days
Laravel delete all rows older than 30 days
In this session, we will try our hand at solving the "Laravel delete all rows older than 30 days".
You can delete all rows from table which are 30 days older from current date. You can use whereDate() and whereRaw() SQL query method to delete the records from table.-
Laravel eloquent to delete 30 days older records from today's date
- whereRaw() SQL Query to delete 30 days older records.
$users = \App\Models\User::whereRaw('DATEDIFF(NOW(), created_at) > 30')->delete(); dd($users);
- whereDate() method to delete 30 days older records from table.
$users = \App\Models\User::whereDate('created_at', '<=', now()->subDays(30))->delete(); dd($users);
It will delete all the records which are 30 days older from today's date and return the number of records as output deleted from table. You can use any of them to get the 30 days older records from the particular table.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- 419 page expired error in Laravel
- Symlink(): No such file or directory
- Comment .env file in laravel
- Display option of select as selected with blade directive Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- If condition in Laravel 9
- Laravel clone model
- How to get the random value form a specific column in laravel ?
- How to get database name in Laravel 9 ?
- Remove public from url in laravel project
- Composer create project laravel/laravel example app
- Get ids in array from users table
- How to get laravel errors folder in views directory in laravel
- Call to a member function update() on null
- Route group with URI prefix using middleware and route name prefixes
- On delete set foreign id column value null using migration in laravel 8
- How to create projects method with belongstomany relationship in user model
- Delete file from amazon s3 bucket using Laravel
- How to use or operator in laravel
- How to create event and listener in laravel ?
- Database transactions in laravel
- Laravel upload file with original file name
- Laravel onclick function not working
- How to add foreign key in laravel using migration