Laravel 8-特性启动不触发

问题描述

我在项目中使用Spatie Media Library。我在media表中添加了列,以供用户ID跟踪谁上传或更新了图片以及图片图片具有与之关联的元数据)。但是media class中的启动方法无法启动。

我的media类是:

<?PHP

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class Media extends BaseMedia
{
    /**
     * Table name
     *
     * @var string
     */
    protected $table = 'media';

    /**
     * Append
     *
     * @var array
     */
    protected $appends = ['url','ext'];

    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        /**
         * Creating the record
         */
        static::creating(function ($obj) {

            $user = auth()->user();

            if (! $user) {
                if (! $obj->creator_id) {
                    $obj->creator_id = 1;
                    $obj->updater_id = 1;
                }
            } else {
                $obj->creator_id = $user->id;
                $obj->updater_id = $user->id;
            }

        });

        /**
         * Updating the record
         */
        static::updating(function ($obj) {

            $user = auth()->user();

            if (! $user) {
                $obj->updater_id = 1;
            } else {
                $obj->updater_id = $user->id;
            }

        });

       /**
        * Global scope to retrieve creator and updater
        */
        static::addGlobalScope('CreatorUpdater',function (Builder $builder) 
        {
            $builder->with('creator','updater');
        });

    }

    /**
     * Get Url
     *
     * @return string
     */
    public function getUrlAttribute()
    {
        return $this->getFullUrl();
    }

    /**
     * Get Ext
     *
     * @return mixed
     */
    public function getExtAttribute()
    {
        $arr = explode('.',$this->file_name);

        return $arr[count($arr) - 1];
    }

    /**
     * Creator
     */
    public function creator()
    {
        return $this->hasOne(User::class,'id','creator_id');
    }

    /**
     * Updater
     */
    public function updater()
    {
        return $this->hasOne(User::class,'updater_id');
    }
}

我错过了什么?如果我使用Log方法boot,则不会获得任何日志条目。

解决方法

啊!我只需要将媒体库指向我的模型即可,而不是Spatie的。

media-library.php配置文件中:

/*
 * The fully qualified class name of the media model.
 */
'media_model' => App\Models\Media::class,