Laravel Delete All Records Older Than 7 Days Example
Hello, all! In this article, we will talk about laravel delete all records older than 10 days. Here you will learn how to delete all records older than 7 days in laravel. we will help you to give an example of laravel delete records older than 7 days. I explained simply about laravel delete old records 7 days example. So, let us see in detail an example.
If you want to keep only last 7 days records in your laravel application then i will give simple database query to delete all records older than 7 days in laravel. you need to just setup in your cron. so, let's see the simple query code.
class PostController extends Controller
* Write code on Method
* @return response()
public function index(Request $request)
Post::whereDate('created_at', '<=', now()->subDays(7))->delete();
I hope it can help you...
We are Recommending you
- How to Add Custom Attribute in Laravel Model?
- Laravel Model Disable Primary Key & Auto Increment Example
- Get Array of Ids from Eloquent Models in Laravel
- Laravel Eloquent Find by Column Name Example
- How to Set Default Value in Laravel Model?
- How to Select Specific Columns in Laravel Eloquent Model?
- Laravel Sum Query with Where Condition Example
- Laravel Eloquent doesntHave() Condition Example
- Laravel Eloquent orWhereHas() Condition Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent firstWhere() Example
- Laravel Model Disable created_at and updated_at Update Record