Reputation: 4539
I am using Spatie Media Library in my project. I have added columns to the media
table for user ids to track who uploaded or updated and image (images have metadata associated with them). But the boot method in the media class
is not firing.
My media
class is:
<?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, 'id', 'updater_id');
}
}
What have I missed? If I Log
in the boot
method I get no log entries.
Upvotes: 0
Views: 283
Reputation: 4539
Argh! I just needed to point the media library to my model and not Spatie's.
In the media-library.php
config file:
/*
* The fully qualified class name of the media model.
*/
'media_model' => App\Models\Media::class,
Upvotes: 0