Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ajvanho's avatar
Level 14

Rule::unique

In earlier versions of Laravel, this worked for me, now: unexpected identifier "users", expecting "]""

'name' => 'required|max:255|Rule::unique('users')->ignore($user)',
0 likes
33 replies
mvd's avatar

Hi @ajvanho

Try

'name' => [
        'required',
	    'max:255',
        Rule::unique('users')->ignore($user->id),
    ],

Don't forget to add use Illuminate\Validation\Rule;

2 likes
Ajvanho's avatar
Level 14

@mvd I dont have an error, but it asks me for unique name!?!

mvd's avatar

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.

1 like
Ajvanho's avatar
Level 14

@mvd This is the problem:

$id = $this->request->get('id');
dd($id) = null

I used this code, I dont know why is $id null

Ajvanho's avatar
Level 14

@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

mvd's avatar

Where are you dumping this code, in your controller?

1 like
Ajvanho's avatar
Level 14

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',
        ];

    }
}
mvd's avatar

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',
        ];
Snapey's avatar

give the full error if you want help

2 likes
Ajvanho's avatar
Level 14
"message": "Method App\Http\Requests\UpdateUserRequest::user does not exist.",
    "exception": "BadMethodCallException"
npispas's avatar

@ajvanho

  1. Make sure you have properly used route model binding.

  2. Try accessing the user like so

$this->user->id

Ajvanho's avatar
Level 14

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);
});
Snapey's avatar

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?

1 like
npispas's avatar

@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',
        ];
1 like
Ajvanho's avatar
Level 14

@snapey Ok, I include: use Illuminate\Http\Request;

but, again I have msg { "name": [ "The name has already been taken." ] }

npispas's avatar

@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.

1 like
Ajvanho's avatar
Level 14

@npispas I use this code earlier, and it works, I dont why is $id = null:

$id = $this->request->get('id');
npispas's avatar
npispas
Best Answer
Level 2

@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" />
1 like
Snapey's avatar

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

2 likes
Snapey's avatar

same problem. You should use Auth:id() if is the user that is changing their own data

Ajvanho's avatar
Level 14

I read that, but I need a solution that works.

Ajvanho's avatar
Level 14

@npispas ignore unique on update... How to find that $user->id in FormRequest ?

Snapey's avatar

two people have sent you the answer

1 like
Ajvanho's avatar
Level 14

@snapey I think this is worked in Laravel 6

$id = $this->request->get('id');
 // dd($id);          is null?!?
 $user = User::find($id);
Snapey's avatar

it will still work if you pass the id in the request data

1 like

Please or to participate in this conversation.