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 specific columns using with method in laravel Eloquent relationship
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Define variable and use in Laravel controller method
- How to show data by ID in laravel?
- How to get path from current URL in Laravel
- How to create event and listener in laravel ?
- How to get only time from created_at in laravel
- Store logged in user details in session and display in view in laravel
- Laravel append URI in route
- Method Illuminate\Http\Request::validated does not exist
- Convert input array to comma-separated string in laravel controller
- Laravel get count with where condition
- Redirect to previous page or url in laravel
- Get the products list ordered by a user
- Laravel 11 project setup on localhost using breeze with blade step by step
- How to check records exist in loaded relationship in Laravel blade view
- Update existing pivot table data in laravel
- How to get date from created_at field in laravel
- How to send ID to another page in Laravel
- How to create projects method with belongstomany relationship in user model
- How to create laravel project using jetstream
- Wheredate in laravel not working
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to pass link from controller to view in laravel on ajax call
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table