Try to use in the middleware:
$request->merge(['customer' => $customer /** the getted customer **/]);
instead of
$request->customer = /** geting customer */;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I created a middleware which can recognize the customer based on my own logic.
So I'm passing the $request->customer variable through the chain to the controller. This works fine with the "Illuminate\Http\Request" request in my controller. But if I want to create a POST route which has a specific Request, like "OrderCreateRequest", the customer property does not arrive.
Middleware:
public function handle(Request $request, Closure $next): Response {
$request->customer = /** geting customer */;
$response = $next($request);
return $response;
}
Validation Request:
class OrderCreateRequest extends FormRequest {
public Customer $customer;
public function rules(): array {
return [/** validation rules */];
}
}
Controller:
class CheckoutController extends Controller {
public function createOrder(OrderCreateRequest $request): RedirectResponse {
dd($request->customer); // null
}
}
Actually I tried to create a new "OrderCreateRequest" request in the middleware in case of this route, but I guess the laravel creates a new one before inject it to the controller. (I check spl_object_hash in the middleware and in the controller and the two hashes was not match.) I think the laravel does not clone the incoming request, create a new one and copy the properties, therefore my customer property has gone.
How should I do this? Thanks for the answers in advance!
Please or to participate in this conversation.