ldc-legaulois/app/Models/Offer.php
2024-07-15 10:25:41 +02:00

76 lines
2.2 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 $title Offer title.
* @property string $location Offer location.
* @property string $contract Offer contract.
* @property string $function Offer function.
* @property string $picture Offer picture.
* @property string $picture_alt Picture alt attribute.
* @property string $picture_title Picture title attribute.
* @property string $description Offer description.
* @property boolean $published Offer publish status.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\Product $products Product polymorphic relationship.
*/
class Offer extends Model
{
use ActivityLog;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'location',
'contract',
'function',
'picture',
'picture_alt',
'picture_title',
'description',
'published',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'published' => 'boolean',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (self $offer) {
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
});
static::updating(function (self $offer) {
$offer->picture = FileStorageHelper::storeFile($offer, $offer->picture, true);
});
static::updated(function (self $offer) {
FileStorageHelper::removeOldFile($offer, 'picture');
});
static::deleted(function (self $offer) {
FileStorageHelper::removeFile($offer, 'picture');
});
}
}