Issue
Normally in Laravel you get a request bag with all parameters in it. But in a service provider this is not the case. But we do have the request()
object.
However, this does not contain the url parameters given in the resource routing.
So what I'm looking for is a way to retrieve the {id} from the route.
I have tried:
request()->route('id')
Route::get('id')
Request::get('id')
All of these return null
Solution
Both boot() and register() methods of any service provider are called before request is parsed and request parameters are known, that's why you can't access them.
Service providers are supposed to register services and shouldn't depend on the request context. If you want to use request parameters in your services, declare request service as a dependency of your service so that it's injected the moment your service gets instantiated.
Answered By - jedrzej.kurylo
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.