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
- Store logged in user details in session and display in view in laravel
- How to validate website url in laravel using validaiton
- How to pass data to partial view file in laravel
- How to return a column with different name in Laravel
- How to make Copy or Duplicate table row in laravel
- Check if Relationship Method Exists in Laravel
- Save or update pivot table data with additional column in Laravel
- Update if exist else insert new record in laravel
- How to insert dynamic value to additional column in pivot table in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Laravel get all records with pagination
- Laravel get single row by id
- How to add active class to menu item in laravel
- How to display 1 day ago in comments in laravel view
- How to add background image to div using Tailwindcss, Vite in Laravel Environment
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- Post model with title and body in laravel 8
- Comment .env file in laravel
- Route group with URI prefix using middleware and route name prefixes
- How to add unique records in pivot columns of Laravel pivot table
- Laravel append URI in route
- How to add dynamic page title in Laravel view
- Best Practices for Error Handling in Production Server Code (example code)
- The use statement with non-compound name 'Auth' has no effect
- Class "App\Http\Controllers\Auth\Verified" not found