
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
- Return redirect laravel not working
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to run a specific seeder class in laravel
- Composer\Exception\NoSslException
- How to add columns in existing table using migration in laravel
- How to get only time from created_at in laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Laravel 9 pagination with search filter
- How to get query string value in laravel
- How to disable timestamps in laravel
- How to call Laravel route in jQuery
- How to create laravel project using composer
- How to create new user without form submission in laravel
- Display message with session flash using bootstrap alert class in laravel
- How to validate website url in laravel using validaiton
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to return error message from controller to view in laravel
- Skip first n record and display rest records in laravel view
- Define variable and use in Laravel controller method
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- How to create and run user seeder in laravel
- Insert values in pivot table dynamically in laravel
- Comment .env file in laravel
- Get laravel version