Never Inject a Controller Into Another Controller
The channel manager pushes reservations to ChannelImportController, and ReservationController::store() already knows how to write one. So the import controller gets ReservationController injected and calls store() once per row. Each call needs a fake Request built with Request::create(). store() validates with the browser form's rules, so a row for a stay that already started fails after_or_equal:today and throws a ValidationException that stops the whole import. It also returns a RedirectResponse nobody reads, and it calls Auth::user(), which inside the import is the staff member whose API token made the push.
Don't do this:
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
final class ChannelImportController extends Controller
{
public function __construct(
private readonly ReservationController $reservations,
) {
}
public function __invoke(Request $request): JsonResponse
{
$rows = $request->array('reservations');
foreach ($rows as $row) {
$fakeRequest = Request::create('/reservations', 'POST', [
'room_id' => $row['room_id'],
'check_in' => $row['arrival'],
'check_out' => $row['departure'],
]);
$this->reservations->store($fakeRequest);
}
return new JsonResponse(['imported' => count($rows)]);
}
}Do this instead
Move the work into a use case class, a class that does one thing the application does and takes plain arguments. Both controllers call it.
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Reservation;
use Carbon\CarbonImmutable;
final class RequestReservation
{
public function handle(
int $guestId,
int $roomId,
CarbonImmutable $checkIn,
CarbonImmutable $checkOut,
): Reservation {
return Reservation::query()->create([
'guest_id' => $guestId,
'room_id' => $roomId,
'check_in' => $checkIn,
'check_out' => $checkOut,
'status' => 'confirmed',
'confirmed_at' => CarbonImmutable::now(),
]);
}
}<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Services\RequestReservation;
use Carbon\CarbonImmutable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
final class ChannelImportController extends Controller
{
public function __construct(
private readonly RequestReservation $requestReservation,
) {
}
public function __invoke(Request $request): JsonResponse
{
$rows = $request->array('reservations');
foreach ($rows as $row) {
$this->requestReservation->handle(
guestId: (int) $row['guest_id'],
roomId: (int) $row['room_id'],
checkIn: CarbonImmutable::parse($row['arrival']),
checkOut: CarbonImmutable::parse($row['departure']),
);
}
return new JsonResponse(['imported' => count($rows)]);
}
}ReservationController::store() keeps its browser rules, calls the same class with the signed-in guest's id, and redirects. The import maps the channel manager's field names onto the arguments. Neither controller knows the other exists. A controller method is the HTTP edge of the application, so work that a second caller needs belongs in a class with no HTTP in it.