ldc-lionor/app/Models/Shop.php

70 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2023-02-24 15:20:58 +00:00
<?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.
*
2024-09-06 08:30:55 +00:00
* @var array<int, string>
2023-02-24 15:20:58 +00:00
*/
protected $fillable = [
'name',
'signs',
'address',
'city',
'postcode',
'latitude',
'longitude',
];
/**
2023-08-28 08:46:47 +00:00
* The attributes that should be cast.
2023-02-24 15:20:58 +00:00
*
2024-09-06 08:30:55 +00:00
* @var array<string, string>
2023-02-24 15:20:58 +00:00
*/
2023-08-28 08:46:47 +00:00
protected $casts = [
2023-02-24 15:20:58 +00:00
'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
2024-09-06 08:30:55 +00:00
self::assertFieldIsUnique('name', $model->name);
2023-02-24 15:20:58 +00:00
});
static::updating(function (self $model) {
// * ASSERTIONS
2024-09-06 08:30:55 +00:00
self::assertFieldIsUnique('name', $model->name, $model->id);
2023-02-24 15:20:58 +00:00
});
}
/**
2023-08-28 08:46:47 +00:00
* Asserts using validation that the field is unique.
2023-02-24 15:20:58 +00:00
*
2023-08-28 08:46:47 +00:00
* @param string $nameField
* @param string $valueField
2023-02-24 15:20:58 +00:00
* @param integer|null $id
* @return void
*/
2023-08-28 08:46:47 +00:00
private static function assertFieldIsUnique(string $nameField, string $valueField, ?int $id = null)
2023-02-24 15:20:58 +00:00
{
$table = (new self())->getTable();
2023-08-28 08:46:47 +00:00
ToolboxHelper::assertFieldIsUnique($table, $nameField, $valueField, $id);
2023-02-24 15:20:58 +00:00
}
}