
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
- Get previous date data in laravel
- How to avoid duplicate entries in pivot table in Laravel
- How to add unique records in pivot columns of Laravel pivot table
- Laravel form request validation
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Permission denied error while creating storage link in Laravel
- Laravel hasmany select not working
- How to get tomorrow and yesterday date in laravel
- How to display HTML tags In Laravel blade
- The use statement with non-compound name 'DB' has no effect
- Redirect to another view from controller in laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to pass data to partial view file in laravel
- Laravel insert query not working
- Drop foreign key column in Laravel using migration
- Cast Array to an Object in Controller and then pass to view in laravel
- Save or update pivot table data with additional column in Laravel
- Retain selected value of select box in Laravel
- Target class [admin] does not exist.
- How to create controller in laravel
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- How to pass two variables in HREF in laravel
- Create model with migration and seeder
- Add a subselect based on relationship using withAggregate method
- How to get session in blade Laravel ?