laravel-admin 删除前回调

作者: JONE 分类: 编程 发布时间: 2020-02-27 20:22
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    public static function boot()
    {
        parent::boot();

        static::deleted(function ($model)
        {
            //这样可以拿到当前操作id
            //dd($model->id);
            // content 字段内容
            //dd($model->content);

            $img = get_html_first_imgurl($model->content,true);

            foreach ($img as $i){
                $url = parse_url($i);

                if(isset($url['path']) ){
                    $file = str_replace(array('/','\\'),DIRECTORY_SEPARATOR,public_path($url['path']));
                    //dump($file);
                    if(is_file($file)) unlink($file);
                }

            }
        });
    }
}