Questions tagged [laravel-service-container]
32 questions
0
votes
0 answers
Why the 'request' key is not in the binding's array in the container when I dd(app())
To my understanding, the 'request' key returned by the getFacadeAccessor method in the Request Facade, refers to the key bound in the service container in the bindings array.
And when I dd(app('request')) I get the Request object. However, when I…

Alphy Gacheru
- 489
- 1
- 9
- 28
0
votes
1 answer
How does Service Container know when a constructor is triggered to resolve its dependencies?
For example I have a class named User and in its constructor I have a dependency named MyDependency.
MyDependency does not have any dependencies itself so I don't need to bind it to he $app container.
When I instantiate the User class with no…

Abw
- 1
- 3
0
votes
2 answers
How can I mock a service class inside a Laravel Job?
I want to mock a service who call a third party, but I can't figure it out.
I have a controller method who has a service injected on it and do stuff:
public function store(Request $request, MyService $myService)
{
$data =…

Nathan Meyer
- 312
- 7
- 18
0
votes
0 answers
Executing my function before loading of kernel (Laravel)
Would like to build in a simple validation for the value of the APP_ENV parameter in my Laravel application. I want the parameter to be either dev, tst, acc or prd. If it's not one of these, my Laravel application should throw an exception and quit.…

Lucas Johnston
- 43
- 1
- 7
0
votes
1 answer
Where to switch to other service depending on logged user field?
In laravel 9 app I try to switch to different service
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
if…

mstdmstd
- 2,195
- 17
- 63
- 140
0
votes
1 answer
Service Provider not Resolving a registration: Target [App\\Services\\Payment\\Contracts\\PaymentService] is not instantiable
I have an interface that I wish to resolve on controllers. On registration inside a service provider, I am binding a class to the Service on condition.
Here is my service interface and two classes.
PaymentService::class
namespace…

John
- 361
- 4
- 12
0
votes
0 answers
Laravel Contextual binding same class with different type of classes in different places
I want to bind same class with different type of classes depending on Controller name
, for example
$this->app->bind(BaseFormRequest::class,function(){
return new CategoryFormRequest;
…

Hassan Elshazly Eida
- 605
- 5
- 20
0
votes
0 answers
Laravel Service won't register
Followed every step from laravel 9 manual, and my service will not register consistently. Don't quite know why but it suddenly breaks and I receive a Target class [thumbnail] does not existerror out of nowhere.
Setup
Service:
namespace…

thomi
- 1,603
- 1
- 24
- 31
0
votes
2 answers
How laravel's helper functions are available globally in every file?
I have just started exploring laravel but I have one confusion . I know how to create own custom function file and make it available globally using compose.json file but I was trying to figure out how laravel's helper function like route() , view()…

Ashu
- 1
- 1
- 2
0
votes
1 answer
Laravel Service VS Trait
Difference between service & trait in Laravel
Assume I have a class which handles image uploading on my application
Should I write it as service or trait ?
when i should use service and trait ?

Red Coder
- 1
- 1
0
votes
0 answers
Laravel: How to register an own implementation of `DatabaseManager` and `Connection`
Due to debugging reasons (I am hunting down a very weird bug), I need to extend the Laravel classes Illuminate\Database\DatabaseManager, Illuminate\Database\Connection, and Illuminate\Database\Query\Builder.
Hence, I created the following three…

user2690527
- 1,729
- 1
- 22
- 38
0
votes
2 answers
Difference between User::class and new User
I am using Laravel 8. I want to know the difference between new User() and User::class because I am having trouble while using new User().
Here is the scenario,
I have UserServices class in which I am injecting UserRepository class
class…

Ahmar Arshad
- 477
- 1
- 10
- 22
0
votes
1 answer
Inject array into Controller with Service Container
I want to provide some default settings to a controller when it is instantiated. My end goal is to provide a dummy object for testing which I can use with registerPaymentMethod on my controller To that end I thought I could provide these via the…

Zakalwe
- 1,444
- 3
- 14
- 25
0
votes
2 answers
Laravel route resolve custom data type
I have the following routes in routes/api.php:
Route::get('items/{item}', function(Guid $item) {...});
Route::get('users/{user}', function(Guid $user) {...});
Since Guid is a custom type, how can I resolve that via dependency injection? As shown,…

shaedrich
- 5,457
- 3
- 26
- 42
0
votes
1 answer
Storing instances in Laravel Service Container that may be null
I'm attempting to store an instance of the current department in Laravels Service Container, however sometimes this department may be null.
The department itself is loaded from session and retrieved as such:
private function…

Sheph
- 625
- 1
- 6
- 19