Home >Database >Mysql Tutorial >Laravel Eloquent ORM in Bangla Part-Pruning Models)
Laravel Eloquent’s model pruning feature allows you to automatically delete outdated or unwanted records in your database. This helps improve performance of large databases and prevents unnecessary data accumulation. In Laravel, you can perform pruning operations through scheduled tasks or Artisan commands.
This method can be used when old records need to be deleted after a specific time. For example, if you want a post to be automatically deleted 30 days after it was created, you can use the trim feature.
prune()
method: Laravel Eloquent 8.x version introduced the prune()
method. This method is designed to delete records based on specific conditions.
<code class="language-php">use App\Models\Post; class Post extends Model { protected static function booted() { static::pruning(function ($query) { // 删除 30 天前创建的帖子 $query->where('created_at', '<', now()->subDays(30)); }); } }</code>
Every time you run Post::prune()
it will delete posts that are 30 days old.
You can use Laravel's task scheduling function to regularly delete old data at a specific time.
schedule()
method. <code class="language-php">use App\Models\Post; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { protected function schedule(Schedule $schedule) { // 每天凌晨 1 点修剪旧帖子 $schedule->call(function () { Post::where('created_at', '<', now()->subDays(30))->delete(); })->dailyAt('01:00'); } }</code>
This will delete posts from 30 days ago every day at 1am.
You can also perform trimming operations manually using Artisan commands. Delete data by creating a new Laravel Artisan command.
<code class="language-bash">php artisan make:command PruneOldPosts</code>
<code class="language-php">namespace App\Console\Commands; use Illuminate\Console\Command; use App\Models\Post; class PruneOldPosts extends Command { protected $signature = 'posts:prune-old'; protected $description = 'Prune posts older than 30 days'; public function handle() { // 删除 30 天前创建的帖子 Post::where('created_at', '<', now()->subDays(30))->delete(); $this->info('Old posts pruned successfully!'); } }</code>
<code class="language-bash">php artisan posts:prune-old</code>
This will run manually and delete posts that are 30 days old.
Sometimes you may want to use soft delete, which means not completely deleting the record, but just marking it as "deleted". In this case, you can combine soft delete and trim functions.
<code class="language-php">use App\Models\Post; class Post extends Model { use SoftDeletes; protected static function booted() { static::pruning(function ($query) { // 修剪 30 天前软删除的帖子 $query->where('deleted_at', '<', now()->subDays(30)); }); } }</code>
This will delete soft-deleted posts that are 30 days old.
When you want to delete records that are related to records, you need to delete those relationships at the same time. For example, if you want when a post is deleted to also delete its associated comments, you can use cascading delete.
<code class="language-php">class Post extends Model { use SoftDeletes; public function comments() { return $this->hasMany(Comment::class); } protected static function booted() { static::deleting(function ($post) { // 删除帖子时删除所有评论 $post->comments()->delete(); }); } }</code>
This will ensure that when a post is deleted its associated comments are also deleted.
The above is the detailed content of Laravel Eloquent ORM in Bangla Part-Pruning Models). For more information, please follow other related articles on the PHP Chinese website!