How to retrieve a url parameter from request in Laravel 5?

Alex Lomia picture Alex Lomia · Aug 3, 2016 · Viewed 29.9k times · Source

I want to perform certain operations with a model in a middleware. Here is an example of what I want to achieve:

public function handle($request, Closure $next)
{
    $itemId = $request->param('item'); // <-- invalid code, serves for illustration purposes only
    $item   = Item::find($itemId);

    if($item->isBad()) return redirect(route('dont_worry'));

    return $next($request);
}

My question is, how can I retrieve the desired parameter from the $request?

Answer

Ilya Yaremchuk picture Ilya Yaremchuk · Aug 3, 2016
public function handle(Request $request, Closure $next)
{
    $itemId = $request->item;
    //..............

}