1

In Laravel 5.1's Routing documentation, it seems it doesn't tell how to get and pass URL parameters to classes. So how should we get route parameters from URLs?

For example, using RESTful Resource Controller to update a user's profile, your app PATCHes (POST actually) to:

/profile/10

So when inside the rules() method in UpdateUserRequest, how do you get the id 10 from the URL to validate user's email as unique from other users like this?

public fnction rules()
{
    return [ 'email' => 'uniqie:users,email,'.$id ]
}

If i dd(Request::all()), it does not contain 'id' = '10'. PLease help. Thank you!

Updates

In the Form Request Validation doc, it said that Laravel grants you to access route/URL parameters with:

$this->route('id');

But it only works on non-resource routes like:

Route::post('users/{id}', 'UsersController@store');

It doesn't work with:

Route::resource('users', 'UserController');

So how do you get and pass route parameters from resource routes?

doncadavona
  • 7,162
  • 9
  • 41
  • 54

1 Answers1

2

Return id to UpdateUserRequest:

$id = $this->route('users');

because Route:resource will give you /users/{users} instead of /users/{id}.

you can use this command via command line :

php artisan route:list

to help you get list all of the route.

Emre Doğan
  • 137
  • 2
  • 11
ssuhat
  • 7,387
  • 18
  • 61
  • 116
  • Thanks. But my problem is passing the $id parameter to Laravel's rule() method inside the UpdateUserRequest class. When I dd(Request::all()), the id is not included in the request. So how would you get the id parameter requested from the URL like: /profile/10? Where 10 is the id of the user to be updated. – doncadavona Jul 19 '15 at 04:30
  • try this. at public function on your controller at first argument $id. ex: public function foocontroller($id, ProfileRequest $request) – ssuhat Jul 19 '15 at 04:34
  • Yes, it's already that way. The problem is passing the id to the ProfileRequest class which contains the rule() method, in which I'd like to use the id in. – doncadavona Jul 19 '15 at 04:48
  • okay. now this one. hope this works: $id = Route::input('id'); put it before return [ ] at your UpdateUserRequest – ssuhat Jul 19 '15 at 04:49
  • Nope. This doesn't work too. Laravel 5.1 returns this error: "Class 'App\Http\Requests\Route' not found". I also mentioned this error as comment at http://stackoverflow.com/questions/28530898/laravel-5-http-requests-pass-url-parameter-to-the-rules-method/28530960?noredirect=1#comment50954028_28530960 – doncadavona Jul 19 '15 at 04:52
  • try : $id = $this->route('id') – ssuhat Jul 19 '15 at 04:58
  • That returns NULL. Because it's the same as Request::get('id'). Which as I've said, the id parameter from the URL doesn't get included in the Request. So still, the problem is how to pass the URL parameters to the rule() method in the UpdateUserRequest. – doncadavona Jul 19 '15 at 05:03
  • My route is simple: Route::resource('users', 'UsersController'); – doncadavona Jul 19 '15 at 05:10
  • That's it! $this->route('users') does it! Sorry I didn't know that resource Route::resource sets the id as the route. Example: Route::resource('users', 'UsersController') will generate routes like 'users/{users}' instead of 'users/{id}'. Thanks @sstarlight! – doncadavona Jul 19 '15 at 13:06
  • Glad can help you. :) vote it if you think its help :D – ssuhat Jul 19 '15 at 13:07
  • Your answer wasn't really the answer so I can't check it as the answer but it led us discussing to the right answer so I'm gonna vote the answer up. Maybe you can edit the answer so it tells the right answer! Thanks again! – doncadavona Jul 19 '15 at 13:11