Level 104
You still need to ignore the current ID; while you could use the String syntax for the unique; it becomes unwieldy to read (especially as an Attribute). You can just implement a more readable rules method instead:
<?php
namespace App\Livewire\Admin;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Rule;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Layout('components.layouts.app')]
#[Title('Category')]
class Category extends Component
{
public $id;
public $title;
public $status;
public function rules()
{
return [
'title' => [
'required',
Rule::unique('categories')->ignore($this->id),
],
'status' => 'required|min:5',
];
}
public function mount()
{
if (request()->routeIs('admin.edit-category')) {
$data = \App\Models\Category::where(['id' => $this->id])->first();
$this->title = $data->title;
$this->status = $data->status;
}
}
public function save()
{
$this->validate();
\App\Models\Category::create([
'title' => $this->title,
'slug' => Str::slug($this->title),
'status' => $this->status,
'user_id' => Auth::user()->id,
]);
session()->flash('success', 'New category added');
$this->redirect('/admin/category', navigate: true);
}
public function edit()
{
$this->validate();
\App\Models\Category::where(['id' => $this->id])->update([
'title' => $this->title,
'status' => $this->status
]);
session()->flash('success', 'Category edited successfully');
$this->redirect('/admin/category', navigate: true);
}
public function render()
{
return view('livewire.admin.category');
}
}
1 like