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'), ]; }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners