Skip to content

Lesson 8 of 22 · Architecture and Code Organization

Laravel Controllers Calling Other Controllers

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

The queries chapter left ReservationController::store() writing the reservation once, with the confirmation code and the total computed before the insert. The queries are fine now. The method still does everything else inline, and this is where the channel manager comes back.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Mail\ReservationConfirmation; use App\Models\Reservation; use App\Models\Room; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; class ReservationController extends Controller { public function store(Request $request): RedirectResponse { $request->validate([ 'room_id' => 'required|exists:rooms,id', 'check_in' => 'required|date|after_or_equal:today', 'check_out' => 'required|date|after:check_in', 'adults' => 'required|integer|min:1', 'children' => 'required|integer|min:0', ]); $guest = Auth::user()->guest; $room = Room::query()->with('roomType')->findOrFail($request->integer('room_id')); $reservation = new Reservation([ 'guest_id' => $guest->id, 'room_id' => $room->id, 'check_in' => $request->input('check_in'), 'check_out' => $request->input('check_out'), 'adults' => $request->integer('adults'), 'children' => $request->integer('children'), 'status' => 'confirmed', 'confirmed_at' => Carbon::now(), 'confirmation_code' => Str::upper(Str::random(8)), ]); $reservation->setRelation('room', $room); $reservation->total_cents = $reservation->calculateTotalCents(); $reservation->save(); Mail::to($guest->email)->send(new ReservationConfirmation($reservation)); return redirect()->route('reservations.show', $reservation); } }

An Artisan command cannot call store(): the method wants a Request, a session, and a logged-in user. So the first version of the import goes the other way round. The channel manager can push new reservations to a URL, the developer exposes one, and the controller behind it does not want to copy the pricing, the confirmation code, and the mail. The method that already does all of that is store(). They inject the controller that owns it.

Borrowing a controller

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class ChannelImportController extends Controller { public function __construct( private readonly ReservationController $reservations, ) { } public function __invoke(Request $request): JsonResponse { $rows = $request->input('reservations', []); foreach ($rows as $row) { $fake = Request::create('/reservations', 'POST', [ 'room_id' => $row['room_id'], 'check_in' => $row['arrival'], 'check_out' => $row['departure'], 'adults' => $row['adults'], 'children' => $row['children'], ]); $this->reservations->store($fake); } return new JsonResponse(['imported' => count($rows)], 200); } }

Laravel's container will build this without complaint. ReservationController is a class with no constructor arguments, so it resolves. The import runs.

Look at what the import had to do to call store(). It built a fake HTTP request per row, because store() reads its input from a Request. It passes the fake through validation rules written for a browser form, so a channel row with a check-in date of yesterday (the channel manager sends stays that already started) fails after_or_equal:today and the whole import stops at that row with an uncaught ValidationException, rendered against the channel manager's request instead of the fake one. It gets a RedirectResponse back that it throws away. And store() still calls Auth::user()->guest, which inside the import is whoever's API token triggered the push, so every imported reservation is attached to the wrong guest, or the row throws on $guest->id when that user has no guest record. The next lesson deals with that line.

A controller method is the HTTP edge of your application. Its job is to take a Request, hand the work to something, and turn the result into a response. When you call it from another controller, you treat the edge as if it were the work. Every assumption inside store() about requests, sessions, redirects, and the logged-in user becomes an assumption the import has to satisfy by faking. The two callers are welded together through a class whose real contract is "I respond to one route".

Pull the work out

The fix is to give the work its own class, a class whose whole job is one thing the application does, named after that thing, taking plain arguments and returning a plain value. Call it a use case class. Requesting a reservation is a thing the application does, so the class is RequestReservation. The name says what the guest does; the property confirms on the spot, which is why the row it writes is already confirmed.

PHP
<?php declare(strict_types=1); namespace App\Services; use App\Mail\ReservationConfirmation; use App\Models\Reservation; use App\Models\Room; use Carbon\CarbonImmutable; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Str; final class RequestReservation { /** * @param array<string, mixed> $attributes */ public function handle(array $attributes): Reservation { $guest = Auth::user()->guest; $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' => app(QuoteService::class)->totalCentsFor($room, $checkIn, $checkOut), ]); Mail::to($guest->email)->send(new ReservationConfirmation($reservation)); return $reservation; } }

The code moved almost unchanged. The pricing call now goes through QuoteService, the class the quote page already uses, because there is no model to price until create() runs. Both callers hand the class an array of attributes and get a Reservation back.

PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Services\RequestReservation; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; class ReservationController extends Controller { public function store(Request $request, RequestReservation $requestReservation): RedirectResponse { $attributes = $request->validate([ 'room_id' => 'required|exists:rooms,id', 'check_in' => 'required|date|after_or_equal:today', 'check_out' => 'required|date|after:check_in', 'adults' => 'required|integer|min:1', 'children' => 'required|integer|min:0', ]); $reservation = $requestReservation->handle($attributes); return redirect()->route('reservations.show', $reservation); } }
PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Services\RequestReservation; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class ChannelImportController extends Controller { public function __construct( private readonly RequestReservation $requestReservation, ) { } public function __invoke(Request $request): JsonResponse { $rows = $request->input('reservations', []); foreach ($rows as $row) { $this->requestReservation->handle([ 'room_id' => $row['room_id'], 'check_in' => $row['arrival'], 'check_out' => $row['departure'], 'adults' => $row['adults'], 'children' => $row['children'], ]); } return new JsonResponse(['imported' => count($rows)], 200); } }

The web controller validates for the browser and redirects. The import controller maps the channel manager's field names onto ours and answers with a count. Neither one knows the other exists, and neither one fakes a request. The rules for what a browser submission must look like stay in the web controller, where they belong; the import can apply its own rules for channel rows, including stays that started yesterday.

Two things about RequestReservation are still wrong, and they are the next two lessons. It reads Auth::user() inside, so it still only works for a logged-in browser user. And it reaches into the container with app(QuoteService::class) and calls Mail:: directly, so nothing in the class tells you what it depends on. Its array $attributes argument is a third problem, and a later lesson is where that gets fixed.

The one thing to remember

A controller is the place where an HTTP request becomes a call into your application. It is not the application. When a second caller needs the same behaviour, the behaviour gets its own class with plain arguments and a plain return value, and both callers call that.

Course Content