lewis4u
lewis4u

Reputation: 15067

How to get the Model id in custom Request class

I have made a custom Request for validation and i don't know how to get the Model id on update.

I'm using route model binding and form model binding but this models id is not shown when i hit this Request for validation and i make

dd($this);

all fields are shown except the model id.

Upvotes: 8

Views: 6593

Answers (2)

Hamidreza
Hamidreza

Reputation: 1915

For example your route is /insurance_contract_items/insurance_contract_item/

You can use $this->insurance_contract_item to get your model instance.

As you read on previous answer you can use also $this->route('insurance_contract_item').

Upvotes: 1

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

use route() method on request to retrive the route parameter

dd($this->route('param_name'));

if your route is like /users/{user_id} then $this->route('user_id'); will give you the parameter user_id value in request if you have bind custom parameter name in route model binding use that parametername in route() method

for ex. Route::model('user', App\User::class); then use $this->route('user'); to retrive the user model directly.

PS. $this means you should be in your Request class where you define rules() and messages() method.

Upvotes: 19

Related Questions