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?
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With