How to Delete All Records Older Than 30 Days in Laravel?
Hi Guys,
In this quick example, let's see laravel delete all records older than 30 days. This example will help you how to delete all records older than 30 days in laravel. I’m going to show you about laravel delete records older than 30 days. This tutorial will give you a simple example of laravel delete old records 30 days example. So, let's follow a few steps to create an example of laravel delete all records older than 30 days.
If you want to keep only last 30 days records in your laravel application then i will give simple database query to delete all records older than 30 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(30))->delete();
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Enum Model Attribute Casting Example
- Laravel 10 Model Observers Example Tutorial
- Laravel 10 Model Events Example Tutorial
- How to Add Custom Attribute in Laravel Model?
- Get Array of Ids from Eloquent Models in Laravel
- Laravel Eloquent Find by Column Name Example
- Laravel Group By with Min Value Query Example
- Laravel Eloquent doesntHave() Condition Example
- Laravel Eloquent orWhereHas() Condition Example
- Laravel Eloquent Group By with Month and Year Example
- Laravel Eloquent When Condition Example
- Laravel Eloquent firstOr() Example
- Laravel Eloquent withMin(), withMax() and withAvg() Example
- Laravel Eloquent withSum() and withCount() Example