Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 325
lewis4u Avatar asked Dec 02 '16 13:12

lewis4u


Video Answer


1 Answers

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.

like image 87
Sagar Rabadiya Avatar answered Sep 17 '22 01:09

Sagar Rabadiya