Automatically remove records using Prunable trait in Laravel
Automatically remove records using Prunable trait in Laravel
Through the use of the programming language, we will work together to solve the "Automatically remove records using Prunable trait in Laravel" puzzle in this lesson.
You can use the Prunable trait to automatically remove models (records) from your database. For example, you can permanently remove soft deleted models after a few days using prunable traits in Laravel.-
Add Prunable trait to the model to remove soft deleted records permanently after a few days
--PATH app\Models\Post.php<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; use Prunable; /** * Determines the prunable query. * * @return \Illuminate\Database\Eloquent\Builder */ public function prunable() { return $this->where('created_at', '<=', now()->subDays(15)); } } //Add PruneCommand to your schedule //app/Console/Kernel.php $schedule->command(PruneCommand::class)->daily();
Sometimes you have to delete the records from tables which are soft deleted and you want to remove it permanently from the table in that case you can use prunable traits.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Laravel 10 Breeze Authentication Example
- How to get route name on visit URL in laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Docker important commands to run laravel application with docker
- Insert Comma Separated Values in laravel
- How to check email is valid or not in Laravel
- How to check records exist in loaded relationship in Laravel blade view
- Display message with session flash using bootstrap alert class in laravel
- Call to a member function pluck() on array
- How to prevent host header attack in Laravel
- How to print form data in laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to update record after save method in Laravel
- Check if Relationship Method Exists in Laravel
- Php artisan make model, factory, migration and controller in single command
- How to add active class to menu item in laravel
- Permission denied error while creating storage link in Laravel
- Route prefix with auth middleware in laravel
- JQuery each loop on json response after ajax in laravel
- If condition in foreach loop in laravel
- Create a record if not exist in laravel
- How to get last month records in Laravel
- Display first n record from collection in laravel view
- Database transactions in laravel
- How to get records in random order in laravel