Laravel 7 handle request validation on update using unique rule but ignore certain unique check

Ee Yeo Keat
3 min readMay 1, 2020
Photo by davisco on Unsplash

This post is just a little sharing about how we can handle validation by using unique rule in requests but ignore certain unique check. For instance, the application has registration form for user to register their username with contact number respectively, then we have create database for store the username and contact number from registered user. However, when user want to update their contact number only without amend their username, it prompts error saying that the username has been taken. It does not make sense, it is the same user who merely want to update the contact number.

Error message sample

This is because we implement unique rule in validation to make sure when the user updates their username which is unique among all the users. There are few discussions provide solutions how to ignoring certain unique checking on a specific column. For Laravel 7.x, there is documentation to show how to force the unique rule to ignore a given ID.

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an “update profile” screen that includes the user’s name, e-mail address, and location. You will probably want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.

To instruct the validator to ignore the user’s ID, we’ll use the Rule class to fluently define the rule. In this example, we’ll also specify the validation rules as an array instead of using the | character to delimit the rules:

use Illuminate\Validation\Rule;

Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);

I would like to see the example of using a rule function in a custom Request file, however, I did not found the example in documentation, therefore, I would like to share with developer who has faced the same issue.

We can copy that entire rule line but with some modification on using the variable named “user”. Otherwise, it will get the error mentioned Undefined variable: user.

In this example, let say we want to allow users to update their contact number only and ignore the same username submission. The lines used in the custom Request file as following:

<?phpnamespace App\Http\Requests\Users;use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateProfileRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'username' => ['required', 'string', 'max:500',
Rule::unique('users', 'username')
->ignore($this->user()->id)],
'contact' => ['required', 'string','min:9'],

];
}
}

Remember to use the custom request file in your controller.

<?phpnamespace App\Http\Controllers;use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\Users\UpdateProfileRequest;
class UsersController extends Controller
{
public function update_profile(UpdateProfileRequest $request)
{
$user = auth()->user();
$user->update([
'username' => $request->username,
'contact' => $request->contact,
]);
session()->flash('success', 'Profile updated successfully.'); return redirect()->back();
}
}

That’s all! Hope this post will contribute little help to those who need it. Just let me know if there is any issue in this code and I am happy to make improvements. Thank you for reading.

My email: eeyeokeat@gmail.com

--

--