Skip to content

WARNING You're browsing the documentation for an old version of Laravel. Consider upgrading your project to Laravel 12.x.

Service Container

Introduction

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

Let's look at a simple example:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Http\Controllers\Controller;
6use App\Repositories\UserRepository;
7use App\Models\User;
8 
9class UserController extends Controller
10{
11 /**
12 * The user repository implementation.
13 *
14 * @var UserRepository
15 */
16 protected $users;
17 
18 /**
19 * Create a new controller instance.
20 *
21 * @param UserRepository