Laravel Delete All Records Older Than 7 Days Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi Friends,

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.

Controller Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

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...

Tags :
Shares