Review this generated Laravel endpoint before deployment.
Let an authenticated customer create an order from validated product quantities, use authoritative prices, persist the order and audit event atomically, and return a public representation.
php
<?php
public function store(Request $request): JsonResponse
{
$payload = $request->all();
$customer = Customer::findOrFail($payload['customer_id']);
$order = Order::create([
'customer_id' => $customer->id,
'status' => 'pending',
]);
foreach ($payload['lines'] as $line) {
$product = Product::findOrFail($line['product_id']);
OrderLine::create([
'order_id' => $order->id,
'product_id' => $product->id,
'unit_price_cents' => $line['unit_price_cents'],
]);
}
AuditEvent::create(['order_id' => $order->id, 'kind' => 'created']);
return response()->json($order->load('customer', 'lines.product'), 201);
}
generated code is illustrative, not from any one model