Check the docs, in the section Forcing A Unique Rule To Ignore A Given ID
Mar 4, 2018
3
Level 1
Checking if a record is already available with exception
Hi guys
I managed to update a record with the data coming from a form.
this is the function
public function update(Language $language) {
$language_data = Language::find($language)->first();
$this->validate(request(), [
'eng_name' => 'required',
'language' => 'required|unique:languages',
'iso' => 'required|max:2|min:2',
'flag' => 'required'
]);
if($language_data) {
$language_data->language = request('language');
$language_data->eng_name = request('eng_name');
$language_data->iso = request('iso');
$language_data->flag = request('flag');
$language_data->update();
}
return redirect('/adm/languages/');
}
The code is working but now I receive an error because if I modify a Language the error that the Language is already taken is throw. So before when I was working on PHP I used to query the mySQL database with something like ... WHERE language = 'Language_from_request' AND id <> $language
How can I replicate this behavior?
Thanks a lot
Level 67
Please or to participate in this conversation.