Hi @ajvanho
Try
'name' => [
'required',
'max:255',
Rule::unique('users')->ignore($user->id),
],
Don't forget to add
use Illuminate\Validation\Rule;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In earlier versions of Laravel, this worked for me, now: unexpected identifier "users", expecting "]""
'name' => 'required|max:255|Rule::unique('users')->ignore($user)',
Hi @ajvanho
Try
'name' => [
'required',
'max:255',
Rule::unique('users')->ignore($user->id),
],
Don't forget to add
use Illuminate\Validation\Rule;
@mvd I dont have an error, but it asks me for unique name!?!
What do you want to achieve / validate? Now you are checking if it is an unique name but ignore the user name from the given user object variable.
@mvd This is the problem:
$id = $this->request->get('id');
dd($id) = null
I used this code, I dont know why is $id null
@mvd I want to update user, but if I dont change name, I dont want to ask me to change that name because is unique
Where are you dumping this code, in your controller?
@mvd in FormRequest class
complete code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\User;
use Illuminate\Validation\Rule;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$id = $this->request->get('id');
dd($id);
$user = User::find($id);
return [
'name' => ['required','max:255',Rule::unique('users')->ignore($user)],
'status' => 'required',
'avatar' => 'required',
];
}
}
You can access the user with
$this->user();
In your rule:
return [
'name' => ['required','max:255',Rule::unique('users')->ignore($this->user()->id)],
'status' => 'required',
'avatar' => 'required',
];
@mvd BadMethodCallException
give the full error if you want help
"message": "Method App\Http\Requests\UpdateUserRequest::user does not exist.",
"exception": "BadMethodCallException"
Make sure you have properly used route model binding.
Try accessing the user like so
$this->user->id
I dont use controller, i use api routes:
Route::put('/users/{id}', function (UpdateUserRequest $request, $id) {
$user = User::find($id);
$user->update($request->validated());
return new UserResource($user);
});
docs;
Since all form requests extend the base Laravel request class, we may use the user method to access the currently authenticated user.
so does your form request class extend the base request?
@ajvanho You can reshape your code like so:
Route::put('/users/{user}', function (UpdateUserRequest $request, User $user) {
$user->update($request->validated());
return new UserResource($user);
});
Then in your UpdateUserRequest class you may access the user like the following:
return [
'name' => ['required','max:255',Rule::unique('users')->ignore($this->user->id)],
'status' => 'required',
'avatar' => 'required',
];
@snapey Ok, I include: use Illuminate\Http\Request;
but, again I have msg { "name": [ "The name has already been taken." ] }
@npispas Nope, Attempt to read property "id" on null"
@ajvanho That's very strange, there's something missing here. I tried the exact same logic as an example and it works.
Api Route:
Route::put('/users/{user}/test', function (\App\Http\Requests\ExampleRequest $request, \App\User $user) {
Log::channel("pms")->debug('hello api route.');
return response()->json([]);
});
FormRequest:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
class ExampleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
Log::channel("pms")->debug('hello example request');
Log::channel("pms")->debug($this->user->id);
return [
//
];
}
}
Logs:
[2021-03-02 09:40:21] local.DEBUG: hello example request
[2021-03-02 09:40:21] local.DEBUG: 1
[2021-03-02 09:40:21] local.DEBUG: hello api route.
Don't mind about the name of the route, I already have that one reserved.
@npispas I use this code earlier, and it works, I dont why is $id = null:
$id = $this->request->get('id');
@ajvanho Because id is in your route parameters and not in the parameter bag of your request.
You need to PUT it if you want to use it like that.
For example use a field like the following:
<input type="hidden" value="{{$user->id}}" name="id" />
@npispas Tnx, It is working.
but this allows any user to change any other's data if you rely on the id being in a hidden field. THIS IS NOT SAFE
Tnx @snapey In the end I use $this->route(‘id’)
same problem. You should use Auth:id() if is the user that is changing their own data
Hi @ajvanho according to Laravel's documentation this could be done as follows
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
You can have a look here https://laravel.com/docs/8.x/validation#rule-unique
two people have sent you the answer
@snapey I think this is worked in Laravel 6
$id = $this->request->get('id');
// dd($id); is null?!?
$user = User::find($id);
it will still work if you pass the id in the request data
Please or to participate in this conversation.