ldc-legaulois/app/Models/SectionApproach.php
2026-03-03 17:22:33 +01:00

63 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class SectionApproach extends Model
{
public const MAX_SECTIONS = 6;
public const MIN_SECTIONS = 2;
protected $table = 'sections_approaches';
protected $fillable = [
'title',
'content',
'picture',
'picture_alt',
'picture_title',
'category_id',
'page_id'
];
protected $casts = [
'category_id' => 'int',
'page_id' => 'int'
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $approach) {
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
static::updating(function (self $approach) {
$approach->picture = FileStorageHelper::storeFile($approach, $approach->picture, true);
});
static::updated(function (self $approach) {
FileStorageHelper::removeOldFile($approach, 'picture');
});
static::deleted(function (self $approach) {
FileStorageHelper::removeFile($approach, 'picture');
});
}
public function category(): BelongsTo
{
return $this->belongsTo(CategoryApproaches::class, 'category_id');
}
public function page(): BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
}