Do you have 2 separate requests for creating and updating?
Jan 7, 2017
16
Level 18
Form request, updating a unique field
I have some validation rules in request classes that have the unique attribute set. Whenever I want to update an existing row in the database, it won't pass, because of this unique attribute.
I've had this issue before, and never was able to get it to work. Tons of topics on the web about this, none of the solutions seem to work. I came up with this:
public function rules()
{
$id = NULL;
if($this->category)
{
$id = $this->category->id;
}
return [
'name' => 'required|alpha_num_spaces|unique:categories,name,' . $id
];
}
I had to do this because when creating a new category, it fails, because the $id (or rather $this->category->id) wasn't set, so I got an error.
The above works. But I hardly think this is the way to go. Any real solution for this?
Please or to participate in this conversation.