Quay lại

Tự động xóa models sử dụng Prunable trait trong Laravel Chuyên mục PHP và Laravel    2023-11-16    7 Lượt xem    3 Lượt thích    comment-3 Created with Sketch Beta. 0 Bình luận

Tự động xóa models sử dụng Prunable trait trong Laravel

Gần đây, mình thấy một tweet của Philo Hermans mô tả cách sử dụng linh hoạt của Prunable trait mới được thêm vào trong Laravel 8.x.

Đơn giản, từ phiên bản 8.50.0, Laravel đi kèm với một Prunable trait. Khi được thêm vào các model, trait này cho phép loại bỏ các model records lỗi thời. hay còn gọi quá thời hạn trong 1 thời gian nào đó.

Prunable trait

Để thực hiện điều này, bạn chỉ cần thêm Illuminate\Database\Eloquent\Prunable trait vào model mà bạn muốn làm prunable. Tiếp theo, bạn cần triển khai phương thức prunable nơi bạn cần viết điều kiện xác định các bản ghi nào cần bị loại bỏ.

Ví dụ, nếu bạn muốn loại bỏ tất cả các bản ghi mô hình cũ hơn 15 ngày, bạn có thể làm như sau.

<?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));
    }
}

Khi đã làm xong điều đó, bạn sau đó cần lên lịch cho lệnh artisan db:prune trong lớp App\Console\Kernel của bạn như sau.

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('db:prune')->daily();
}

Điều này sẽ cho phép Laravel quét và phát hiện tất cả các models có Prunable trait dưới thư mục app/models và loại bỏ các bản ghi của mô hình tương ứng.

pruning method

Bây giờ, có thể có một tình huống nơi các model records có thể có một số resources bổ sung đi kèm. Rõ ràng, khi các bản ghi model records đang bị loại bỏ, bạn cũng muốn các resources liên quan này bị xóa theo.

Vì mục đích chính xác này, phương thức pruning có thể được sử dụng cùng với phương thức prunable. Phương thức này sẽ được gọi trước khi model record bị xóa.

Ví dụ, nếu bạn muốn các tệp S3 liên quan được xóa cùng với các model records, bạn có thể làm như sau.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

class File 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));
    }

    /**
     * Prepare the model for pruning.
     *
     * @return void
     */
    protected function pruning()
    {
        // Remove the associated file from S3 before deleting the model
        Storage::disk('s3')->delete($this->filename)
    }
}

 

Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough

Bài viết liên quan

Learning English Everyday