ldc-qrcollector-back/app/Models/Url.php
2026-06-18 10:59:44 +02:00

79 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Uwil\LaravelCommon\Helpers\FileHelper;
class Url extends Model
{
use HasFactory;
use ActivityLog;
/**
* The attributes that are mass assignable.
*
* @var list<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' => 'array',
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function boot(): void
{
static::creating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::updating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::updated(function (self $model) {
FileHelper::removeOldFile($model, 'logo');
});
static::deleted(function (self $model) {
FileHelper::removeFile($model, 'logo');
});
parent::boot();
}
/**
* Get the brand that owns the url.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Brand, $this>
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* Get the history for the url.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\UrlHistory, $this>
*/
public function history()
{
return $this->hasMany(UrlHistory::class);
}
}