
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
- How to get list of all views file in laravel
- Ignore Records where a field has NULL value in Laravel
- How to increment column value of table in Laravel
- How to change default timestamp fields name in Laravel
- If condition in Laravel 9
- Show old value while editing the form in Laravel
- Laravel pagination links with query string
- Update email with unique validation in laravel
- Add class to body in laravel view
- First and last item of the array using foreach iteration in laravel blade
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- How to restore deleted records in laravel
- How to display order by null last in laravel
- How to upload files to amazon s3 bucket using Laravel
- How to get random string in Laravel
- How to update record after save method in Laravel
- Eager loading dynamically in laravel
- The POST method is not supported for this route. Supported methods: PUT.
- Store logged in user details in session and display in view in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- Target class [HomeController] does not exist
- How to print form data in laravel
- Comment .env file in laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist