Rohan Rohan - 4 months ago 32
PHP Question

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?

Answer

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

public function authorize(){
    $id = $this->route('id');
}