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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners