Skip to content

Lesson 10 of 22 · Architecture and Code Organization

Laravel Dependency Injection vs Facades and app()

Intermediate•
lesson•
November 30, 2026•
~4 min read

Open RequestReservation and try to answer one question: what does this class need in order to run? The constructor is missing, which suggests the answer is "nothing". The body says otherwise. Halfway down handle() there is app(QuoteService::class), and a few lines later Mail::to(...). The class needs a pricing service and a mailer. It just never says so where you would look.

Two spellings of the same habit

app(QuoteService::class) reaches into Laravel's container at the moment the class needs something and pulls it out. That is service location: the class locates its own services, at runtime, from inside a method. Laravel makes it a one-liner with app(), resolve(), and the App facade.

Mail::to($guest->email) looks different and is the same thing. A facade is a static front for a container binding. When you call Mail::to(), the facade asks the container for the mail manager and forwards the call to it. Nicer syntax, same runtime lookup from inside the body, same invisibility from the outside.

Both spellings have the same two costs.

The class lies about what it needs. new RequestReservation() takes no arguments, so a reader assumes it is self-contained. To learn the truth they read every method. When the class grows, the lookups spread through it, and the list of real dependencies exists nowhere.

Tests have to go through the container. You cannot hand the class a fake mailer, because the class does not take one. You bind the fake into the container before the test runs.

Ask for what you need in the constructor

Constructor injection is the other way round: the class declares its dependencies as constructor parameters, and whoever builds the class supplies them. That is dependency injection, and you have done it already: OccupancyReport type-hints ConnectionInterface in its constructor. When Laravel's container builds the class it reads the parameter types and passes an instance of each, so the controllers that already type-hint RequestReservation keep working with no change. The difference is that the dependencies are now written down in one place.

The mail dependency is declared as Illuminate\Contracts\Mail\Mailer, which the container resolves to the default mailer behind the mail manager that the facade forwards to.

PHP
<?php declare(strict_types=1); namespace App\Services; use App\Mail\ReservationConfirmation; use App\Models\Guest; use App\Models\Reservation; use App\Models\Room; use Carbon\CarbonImmutable; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Support\Str; final readonly class RequestReservation { public function __construct( private QuoteService $quotes, private Mailer $mailer, ) { } /** * @param array<string, mixed> $attributes */ public function handle(int $guestId, array $attributes): Reservation { $guest = Guest::query()->findOrFail($guestId); $room = Room::query()->with('roomType')->findOrFail($attributes['room_id']); $checkIn = CarbonImmutable::parse($attributes['check_in']); $checkOut = CarbonImmutable::parse($attributes['check_out']); $reservation = Reservation::query()->create([ 'guest_id' => $guest->id, 'room_id' => $room->id, 'check_in' => $checkIn, 'check_out' => $checkOut, 'adults' => $attributes['adults'], 'children' => $attributes['children'], 'status' => 'confirmed', 'confirmed_at' => CarbonImmutable::now(), 'confirmation_code' => Str::upper(Str::random(8)), 'total_cents' => $this->quotes->totalCentsFor($room, $checkIn, $checkOut), ]); $this->mailer->to($guest->email)->send(new ReservationConfirmation($reservation)); return $reservation; } }

The class became readonly as well as final. Its two properties are set once in the constructor and never change, and marking the class readonly makes PHP enforce that. Str::random() stays: Str is a plain helper class with static methods, not a facade, and nothing is resolved from the container to call it.

Now the constructor answers the question the lesson opened with. RequestReservation needs a QuoteService and a Mailer. That is the whole list.

The test that becomes possible

The test builds the class by hand and passes it what it needs. Mail::fake() returns a MailFake, and MailFake implements the Mailer contract, so it can go straight into the constructor. Facades are fine in a test; the rule is about production classes.

PHP
<?php declare(strict_types=1); namespace Tests\Feature\Reservations; use App\Mail\ReservationConfirmation; use App\Models\Guest; use App\Models\Room; use App\Services\QuoteService; use App\Services\RequestReservation; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Mail; use Tests\TestCase; final class RequestReservationTest extends TestCase { use RefreshDatabase; public function test_it_creates_a_confirmed_reservation_and_mails_the_guest(): void { $guest = Guest::factory()->create(['email' => 'ana@example.com']); $room = Room::factory()->create(); $mailer = Mail::fake(); $requestReservation = new RequestReservation(new QuoteService(), $mailer); $reservation = $requestReservation->handle( guestId: $guest->id, attributes: [ 'room_id' => $room->id, 'check_in' => '2026-10-03', 'check_out' => '2026-10-06', 'adults' => 2, 'children' => 0, ], ); $this->assertSame('confirmed', $reservation->status); $this->assertSame($guest->id, $reservation->guest_id); $this->assertDatabaseHas('reservations', ['id' => $reservation->id, 'room_id' => $room->id]); $mailer->assertSent(ReservationConfirmation::class); } }

Nothing in the test touches the container to set the class up. There is no actingAs(), because the guest id is an argument. There is no $this->app->instance(...), because the mailer is an argument. Read the new RequestReservation(...) line and you know everything the class depends on, which is the property the old version lacked.

One dependency in that constructor is still a problem. QuoteService gets its pricing formula from a trait, and the model has the same trait. The next lesson pulls the rule out of both.

The one thing to remember

A class states what it needs in its constructor, and the container fills it in. app() calls and facades inside a method hide the same dependencies where nobody can see them, and the first thing that hiding costs you is the ability to test the class without global setup.

Course Content