On https://goWithPHP.com, I presented code that places an order in a PHP-based system. The goal was to show how the language has evolved over time for those who hadn't seen PHP code in a long time.
Using this user instance, we can check if the user is unauthorized to place orders, abort the request and respond with a 403 status code.
When a new order is created, the system has to send the order to the vendor for processing.
Here's the full code after refactoring and extracting some methods: class OrdersController extends Controller { public function store(Request $request) { abort_if($request->user()->cannot('place-order', Order::class), 403); $validated = $this->validateOrder($request); $order = $this->placeOrder($request, $validated); SendOrderToVendor::dispatch($order)->onQueue('orders'); NewOrderPlaced::dispatch($order); return [ 'order' => $order->load('products'), ]; }}