ldc-lionor/app/Models/Shop.php
2023-08-28 08:46:47 +00:00

69 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\SignsEnum;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'signs',
'address',
'city',
'postcode',
'latitude',
'longitude',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'signs' => SignsEnum::class,
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
// * EVENT CLOSURES.
static::creating(function (self $model) {
// * ASSERTIONS
static::assertFieldIsUnique('name', $model->name);
});
static::updating(function (self $model) {
// * ASSERTIONS
static::assertFieldIsUnique('name', $model->name, $model->id);
});
}
/**
* Asserts using validation that the field is unique.
*
* @param string $nameField
* @param string $valueField
* @param integer|null $id
* @return void
*/
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
}
}