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

hivokas's avatar

How to get ID inside custom Request?

Hello! I have update method in EmployeesController and I want to validate the input data. So I use own StoreEmployeeRequest.

public function update(StoreEmployeeRequest $request, $id)
    {
        //
    }

Inside StoreEmployeeRequest.php I have rules() method, where I put all rules.

public function rules()
    {
        return [
            'surname' => 'required|max:50',
            'name' => 'required|max:50',
            'patronymic' => 'required|max:50',
            'passport' => 'required|min:6|max:30|unique:employees,passport'
            // and some more rules  
        ];
    }

passport must be unique, so this validation is OK for creating new row, BUT I need to update existing one.

I read that I need to pass third param to unique validation rule that equals to id like below:

'passport' => 'required|min:6|max:30|unique:employees,passport,' . $id

And here is the problem. How can I get id inside StoreEmployeeRequest.php?

I saw this solution: https://laracasts.com/discuss/channels/requests/laravel-5-validation-request-how-to-handle-validation-on-update/replies/4310

First variant is working (but for some reason I think that it's now a good solution):

$request->segment(2)

Are there any other solutions to get id, because the second variant from the link above isn't working :(

$request->get('id')
0 likes
11 replies
Snapey's avatar

$this->id because the form request extends request

(assuming you passed the id in the request)

1 like
hivokas's avatar

@Snapey I have.

PUT|PATCH | employees/{employee} | employees.update | App\Http\Controllers\EmployeesController@update (line from php artisan route:list)

Snapey's avatar

sorry, try $this->route('id') if it's in the url

Snapey's avatar
Snapey
Best Answer
Level 122

sorry, try $this->route('employee') as that is what you called it in the url

5 likes
steveb's avatar

For a custom FormRequest and Laravel 5.7+ you can get the id of your updated model like this:

public function rules()
    {
        return [
            'name' => 'required|min:5|max:255|unique:schools,name,'.\Request::instance()->id
        ];
    }
2 likes
moehtet's avatar

$this->route('parse-value') saved my life. Thanks. Snapey

Yazan Salhi's avatar

Try this

'passport' => 'required|min:6|max:30|unique:employees, passport,'.$this->id,

Please or to participate in this conversation.