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
- How to check if user has created any post or not in laravel
- Send OTP using textlocal api in laravel
- How to display serial number in Laravel?
- Create project table with model and migration
- Always load the relationship data with eager loading in Laravel
- How to upload local Laravel project to server ?
- How to access the nth object from Laravel collection object ?
- How to create and run user seeder in laravel
- Laravel 9 route group with controller
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to get route method name in Laravel
- How to upload multiple images after preview in laravel using cropper js
- Sample .htaccess file and index.php file under public directory in laravel
- Use withCount() to Calculate Child Relationship Records
- Display first n record from collection in laravel view
- How to call controller function from view in Laravel
- Send id with route Laravel
- How to display order by null last in laravel
- Print query in laravel
- How to validate URL with https using regex in laravel
- Return redirect laravel not working
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Class "App\Http\Controllers\Auth\Verified" not found
- How to restore multiple records after soft-deletes in Laravel