ldc-mesbr-app/app/Models/Brand.php

86 lines
1.8 KiB
PHP
Raw Normal View History

2023-09-13 16:48:17 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Lib\Helpers\FileStorageHelper;
class Brand extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'contact_mail',
'rcs',
'logo',
'crm_token',
'crm_password'
];
/**
2023-11-24 15:35:08 +01:00
* The attributes that should be cast.
*
* @var array
2023-09-13 16:48:17 +02:00
*/
2023-11-24 15:35:08 +01:00
protected $casts = [
'crm_token' => 'encrypted',
'crm_password' => 'encrypted',
];
2023-09-13 16:48:17 +02:00
/**
2023-11-24 15:35:08 +01:00
* Get campaign that owned by the Brand
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2023-09-13 16:48:17 +02:00
*/
2023-11-24 15:35:08 +01:00
public function campaigns(): HasMany
2023-09-13 16:48:17 +02:00
{
2023-11-24 15:35:08 +01:00
return $this->hasMany(Campaign::class);
2023-09-13 16:48:17 +02:00
}
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'logo');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'logo');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
$model->logo = FileStorageHelper::storeFile($model, $model->logo, true);
}
}