fix: 🐛 add getKey() in the booted method for NewsCategory model

This commit is contained in:
Nicolas Demangeat 2024-08-30 17:53:43 +02:00 • committed by Alexis Gousseau
parent f5109fee1a
commit ba8cfb48e3

View file

@ -2,7 +2,6 @@
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -52,14 +51,14 @@ class NewsCategory extends Model
*/
protected static function booted(): void
{
static::creating(function (self $newsCorporate) {
self::setOrder($newsCorporate);
self::assertLabelIsUnique($newsCorporate->title);
self::manageFields($newsCorporate);
static::creating(function (self $newsCategory) {
self::assertTitleIsUnique($newsCategory->title);
self::setOrder($newsCategory);
self::manageFields($newsCategory);
});
static::updating(function (self $newsCorporate) {
self::assertLabelIsUnique($newsCorporate->title);
self::manageFields($newsCorporate);
static::updating(function (self $newsCategory) {
self::assertTitleIsUnique($newsCategory->title, $newsCategory->getKey());
self::manageFields($newsCategory);
});
}
@ -68,12 +67,12 @@ class NewsCategory extends Model
/**
* Set model's order after the last element of the list.
*
* @param self $mainSlide
* @param self $model
* @return void
*/
private static function setOrder(self $mainSlide): void
private static function setOrder(self $model): void
{
$mainSlide->order = \intval(self::query()->max('order')) + 1;
$model->order = \intval(self::query()->max('order')) + 1;
}
/**
@ -88,16 +87,17 @@ class NewsCategory extends Model
}
/**
* Asserts using validation that the label is unique
* Asserts using validation that the title is unique
*
* @param string|null $label
* @param string|null $title
* @param integer|null $id
* @return void
* @throws \Illuminate\Validation\ValidationException If a meat label already exists.
* @throws \Illuminate\Validation\ValidationException If an news category title already exists.
*/
private static function assertLabelIsUnique(string|null $label, ?int $id = null)
private static function assertTitleIsUnique(string|null $title, ?int $id = null): void
{
$table = (new self())->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($label), $id);
ToolboxHelper::assertFieldIsUnique($table, 'title', $title, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($title), $id);
}
}