87 lines
1.8 KiB
PHP
87 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Url extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'key',
|
|
'value',
|
|
'title',
|
|
'image_settings',
|
|
'logo',
|
|
'brand_id'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'image_settings' => 'json',
|
|
];
|
|
|
|
/**
|
|
* The "booted" method of the model.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function boot(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
|
|
});
|
|
static::updating(function (self $model) {
|
|
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'logo');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'logo');
|
|
});
|
|
parent::boot();
|
|
}
|
|
|
|
/**
|
|
* Get the brand that owns the url.
|
|
*
|
|
* @return BelongsTo
|
|
*/
|
|
public function brand()
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
/**
|
|
* Get the history for the url.
|
|
*
|
|
* @return HasMany
|
|
*/
|
|
public function history()
|
|
{
|
|
return $this->hasMany(UrlHistory::class);
|
|
}
|
|
|
|
/**
|
|
* Get the stats for the url.
|
|
*
|
|
* @return HasMany
|
|
*/
|
|
public function stats()
|
|
{
|
|
return $this->hasMany(UrlStats::class);
|
|
}
|
|
}
|