ItSolutionStuff.com

Laravel Delete All Records Older Than 7 Days Example

By Hardik Savani • November 5, 2023
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Custom Attribute in Laravel Model?

Read Now →

Laravel Model Disable Primary Key & Auto Increment Example

Read Now →

Get Array of Ids from Eloquent Models in Laravel

Read Now →

Laravel Eloquent Find by Column Name Example

Read Now →

How to Set Default Value in Laravel Model?

Read Now →

How to Select Specific Columns in Laravel Eloquent Model?

Read Now →

Laravel Sum Query with Where Condition Example

Read Now →

Laravel Eloquent doesntHave() Condition Example

Read Now →

Laravel Eloquent orWhereHas() Condition Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Eloquent firstWhere() Example

Read Now →

Laravel Model Disable created_at and updated_at Update Record

Read Now →