Laravel Delete All Records Older Than 7 Days Example
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...
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
- 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