
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
- Get all users except the followings users in overtrue laravel-follow
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Laravel 9 pagination with search filter
- How to get the random value form a specific column in laravel ?
- Trying to access array offset on value of type null error in laravel
- Composer create project laravel/laravel example app
- How to pass variable from controller to model in Laravel
- Laravel file size validation not working
- How to add dynamic page title in Laravel view
- There are no commands defined in the "route:" namespace
- How to check record exist or not in relationship table
- Include External CSS and JS file in Laravel
- How to validate website url in laravel using validaiton
- Redirect to another view from controller in laravel
- Display message with session flash using bootstrap alert class in laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Get Array of IDs from Eloquent Collection
- Update existing pivot table data in laravel
- How to call controller function from view in Laravel
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- How to make Copy or Duplicate table row in laravel
- Laravel save object to database
- Insert dummy data in users table Laravel
- Define variable and use in Laravel controller method
- How to Get records between two dates in Laravel