Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the request route parameter in Laravel 5 form request?

I am new to Laravel 5 and I am trying to use the new Form Request to validate all forms in my application.

Now I am stuck at a point where I need to DELETE a resource and I created a DeleteResourceRequest for just to use the authorize method.

The problem is that I need to find what id is being requested in the route parameter but I cannot see how to get that in to the authorize method.

I can use the id in the controller method like so:

public function destroy($id, DeletePivotRequest $request) {     Resource::findOrFail($id); } 

But how to get this to work in the authorize method of the Form Request?

like image 698
Rohan Avatar asked May 05 '15 11:05

Rohan


People also ask

How do you pass a route parameter in Laravel?

Laravel routes are located in the app/Http/routes. For instance, to pass in a name parameter to a route, it would look like this. Route::get('/params/{name}', function ($name) { return $name }); By convention, the Controller function accepts parameters based on the parameters provided.

What is request -> input () in Laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

What are routes in Laravel explain Route handling with controller and route parameters?

You can define a route to this controller action, as: Route::get('user/{id}', 'UserController@show'); Route::resource: The Route::resource method can be a Restful Controller that produces all the basic routes required for an application and is dealt via controller class.


2 Answers

That's very simple, just use the route() method. Assuming your route parameter is called id:

public function authorize(){     $id = $this->route('id'); } 
like image 138
lukasgeiter Avatar answered Oct 02 '22 07:10

lukasgeiter


You can accessing a Route parameter Value via Illuminate\Http\Request instance

public function destroy($id, DeletePivotRequest $request) {     if ($request->route('id'))     {         //     }      Resource::findOrFail($id); } 
like image 26
Emeka Mbah Avatar answered Oct 02 '22 07:10

Emeka Mbah