Skip to content

Can't use boot() nor mount() to make Dependency Injection of service class #1193

Closed Answered by Aeropixel
Aeropixel asked this question in Q&A
Discussion options

You must be logged in to vote

Alright, finally I've found a solution to my problem!

To finally solve the "xxxService must not be accessed before initialization" and rightfully use my services I did this:

    private function initialize(): void
    {
        if (!isset($this->userService)) {
            $this->userService = app(UserService::class);
        }
    }

then I call it on builder():

    public function builder(): Builder
    {
        $this->initialize(); // Initialize the $userService method

        return (!$this->withRole) ?
            $this->userService->getUsersNotWithRole($this->role->id) :
            $this->userService->getUsersWithRole($this->role->id);
    }

Now I can use the services I injected…

Replies: 2 comments 7 replies

Comment options

lrljoe
May 12, 2023
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
7 replies
@Aeropixel
Comment options

@lrljoe
Comment options

lrljoe May 21, 2023
Collaborator Sponsor

@Aeropixel
Comment options

@Aeropixel
Comment options

Answer selected by Aeropixel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants