Loading…

What is Dependency injection in Laravel?

In Laravel, dependency injection is a term used for the activity of injecting components into the user application. It’s a key element of agile architecture. 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