Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 : get route parameter in Controller 's constructor

I defined routes for a controller this way :

/model/{id}/view
/model/something/{id}/edit

I need to get the id parameter in the contructor of this controller. For example :

class ArtController extends Controller {

  public function __construct(Request $request){
    //dd($this->route('id'));  //Doesn't work
    //dd($request->segments()[1]); //this works for the first route but not the second
  }
}

How can you get the parameter id in the constructor of a Controller in Laravel?

like image 772
BassMHL Avatar asked Jun 13 '15 06:06

BassMHL


1 Answers

You should be able to do something like this

$id = Route::current()->getParameter('id');

Update:

Starting in laravel 5.4 getParameter was renamed to parameter

$id = Route::current()->parameter('id');
like image 184
Rob Avatar answered Oct 14 '22 16:10

Rob