Loading…

What is Dependency injection used in Laravel?

Dependency injection is a technique that essentially means this: class dependencies are "injected" into the class via the constructor, or it’s "setter" methods. The Laravel service container is a powerful tool that manages all class dependencies and performs dependency injection.

public function __construct(UserRepository $data)
{
    $this->userdata = $data;
}

In this given an example, the UserController needs to retrieve users data from a data source(database). So, we can inject a service that is able to recover all users. In this example, our UserRepository most likely uses Eloquent to get user’s data from the database.

Suggest An Answer

Our Particitants

The Service Container in Laravel is a Dependency Injection Container and a Registry for the application.

Dependency injection is commonly used in Laravel. Even access to Request we mostly inject it.

public function __construct(Request $request){

}

Latest post from Laravel Interview Questions

Ask Question