Delete All Records Older Than 7 Days In laravel

Posted by

Dear all, We shall discuss laravel delete all records older than 10 days in this article. You can discover how to delete any records in Laravel that are more than seven days old here. We will assist you by providing a sample of how to delete records older than seven days in Laravel. I gave a clear explanation of the laravel delete old records after 7 days example. So, let’s examine an example in more detail.

I’ll provide a straightforward database query to erase all entries older than 7 days in your Laravel application if you just want to maintain the most recent 7 days’ worth of data. All you have to do is set up your cron. So let’s examine the straightforward query code.

controller

<?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();
  
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x