You've only shared a snippet of your code. What exactly is your question?
Feb 11, 2024
3
Level 25
Displaying error at update for unique
class Create extends Component { #[Rule('required', message: 'Name is required')] #[Rule('unique:products,name', message: 'Product name already available', onUpdate: false)] public $name;
#[Rule('required', message: 'Status is required')]
public $status;
public $type;
public $id;
public function mount()
{
if ($this->type == 'edit') {
$data = Product::where('id', $this->id)->first();
$this->name = $data->name;
$this->status = $data->status;
}
}
public function save()
{
$this->validate();
$faker = Factory::create();
Product::create([
'name' => $this->name,
'status' => $this->status,
'color' => $faker->hexColor(),
'created_user_name' => \Auth::user()->name,
'user_id' => \Auth::user()->id,
]);
return redirect()->route('products')->with('success', 'New product added');
}
public function update()
{
$this->validate();
Product::find($this->id)->update([
'name' => $this->name,
'status' => $this->status,
]);
return redirect()->route('products')->with('success', 'Product data updated');
}
public function render()
{
return view('livewire.admin.products.create');
}
}
Please or to participate in this conversation.