In this episode, we're diving into route model binding within action classes – treating actions a bit like controllers to show how seamlessly things can work. We kick off by setting up a new feature: a simple form button on a dashboard that, when clicked, creates an order (think basic e-commerce, but we're keeping it super simple for now).
To make this happen, we spin up a new Order
model, migration, and factory, making sure each order belongs to a user. We then migrate our changes and update the User
model so we can easily access a user's orders.
Next, we create a new action called CreateOrder
and set up a more RESTful route structure like /users/{user}/orders
. This is where route model binding really shines: we pass the user through the route, and Laravel will automatically resolve the user instance for us in the action just like it does in a traditional controller.
After hooking the button up to this new route on our dashboard, we test it out. When clicked, an order is created for the current user just as expected! We wrap things up by emphasizing how actions can accept models via route model binding just like controllers, making them flexible and powerful for organizing your app's logic.
It's a straightforward episode, showing you that you can take advantage of all the Laravel routing features you already know even when you're building your app with actions!