ldc-agis/app/Models/Explorerpage.php

83 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property string $picture_1 Path of the picture 1.
* @property string $picture_1_alt Picture 1 alt attribute.
* @property string $picture_1_title Picture 1 title attribute.
* @property string $picture_2 Path of the picture 2.
* @property string $picture_2_alt Picture 2 alt attribute.
* @property string $picture_2_title Picture 2 title attribute.
* @property string $picture_3 Path of the picture 3.
* @property string $picture_3_alt Picture 3 alt attribute.
* @property string $picture_3_title Picture 3 title attribute.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Commitment $commitments Commitments Many-to-many relationship.
*/
class Explorerpage extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'picture_1',
'picture_1_alt',
'picture_1_title',
'picture_2',
'picture_2_alt',
'picture_2_title',
'picture_3',
'picture_3_alt',
'picture_3_title',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function () {
throw new \RuntimeException("Creating a new page is not authorized");
});
static::updating(function (self $model) {
$model->picture_1 = FileStorageHelper::storeFile($model, $model->picture_1, true);
$model->picture_2 = FileStorageHelper::storeFile($model, $model->picture_2, true);
$model->picture_3 = FileStorageHelper::storeFile($model, $model->picture_3, true);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'picture_1');
FileStorageHelper::removeOldFile($model, 'picture_2');
FileStorageHelper::removeOldFile($model, 'picture_3');
});
static::deleting(function () {
throw new \RuntimeException('Deleting a page is not authorized');
});
}
// * RELATIONS
/**
* The commitments that belong to the page.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function commitments(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(Commitment::class);
}
}