This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
06. Route model binding in actions

Transcript

00:00
We're going to create out a completely new action now
00:02
to demonstrate root model binding within actions when we're using them like controllers. Now, this is actually very easy. But the action that we're going to be creating,
00:10
we're going to use in the next few episodes when we look at authorization. OK, so I've swapped out over on the dashboard this for a very simple form with just a button.
00:20
This is going to create an order. Imagine that this is some sort of e-commerce platform. Of course, it's just a button at the moment, and we're just going to invoke an action.
00:28
So we're going to go and just create an entirely new set of data here. So let's go ahead and make out a model called order. Again, we'll generate a migration and a factory
00:37
alongside that. And we'll just fill out the orders table. And again, we're just going to keep this super simple. We're not actually going to add any data in here apart
00:45
from the fact that this belongs to a user, just so we know that this works. OK, so that's pretty much all we're doing for our order. Let's go ahead and migrate this data.
00:55
And we will hop over to the user model. And let's just copy this down to be lazy, and change this to orders, and change this to our order model. OK, great.
01:04
So now we can just start to create out a new action which creates an order. So once again, we can do this on the command line. We're going to make an action.
01:13
And as you guessed, we're going to call this create order. Now, before we do anything with the action itself, let's look at the web route for this. We're going to make this slightly different.
01:23
Rather than be slash orders, we're going to make this a little bit more restful. And we're going to let the orders belong to a user within that chain inside of our URL.
01:34
So we're going to say slash users slash the user in here using route model binding, and then orders. And of course, this is going to be create order. Now, the route model binding that we're going to look at
01:46
works with anything in any URL you create. This is just an example. OK, so let's go ahead and hook this up to our dashboards. And if we just go to the action here, of course,
01:57
we need to swap this out. What we can do, of course, is just pluck the ID out of the currently authenticated user and add it in here.
02:05
But what I'm going to do just to make this a little bit better is just go ahead and say users.orders, just so we have a reference to that and we can generate this route out a little bit more easily.
02:15
So in here, we're just going to output using the route helper users orders. And we're just going to pass the currently authenticated user into that.
02:26
That will generate the route for us. And if we go ahead and create this, you can see we're going through to slash users slash one slash orders.
02:33
OK, so now what we want to do is within our action itself, which is create order, actually fill this in. Now, the handle method at the moment has nothing in there. But we know we want to receive a user into that.
02:47
Of course, we probably would want to receive an array of data. But at the moment, since we're not storing any additional data in the database,
02:53
we're just going to leave this as a user. And we're going to create an empty order. So for our action, this does one thing. It creates an order for a user.
03:02
So we're going to say user orders and create. And we're just going to pass an empty array in here. Now, we know we're using this as a controller. And we already know how to do this.
03:13
We're going to call or create a method called as controller. And we're going to go ahead and call this handle. And we're going to pass through the user. Now, this is where this gets a little bit trickier.
03:26
Because the user that we're trying to access doesn't come from the currently authenticated user. This comes from the user that we're passing in via root model binding, whether that's
03:37
the ID of the user, the username, or whatever else. So how do we do this? Well, we do exactly what we would do in a normal controller.
03:45
We just accept it in. That's pretty much it. So let's go and pass that user through to handle and see what happens.
03:53
So let's come over, click Create Order. And we aren't redirecting anywhere. But if we head over to the Orders table here, you can see that that order has been created.
04:03
So basically, this does work like a controller in the sense that any model that we want to use root model binding for will just get picked up in exactly the same way as if we were to do this in a normal controller.
04:15
And then, of course, we can defer this to the handle method. Any of the items that we get passed through with root model binding can then be used to do what we need within our action.

Episode summary

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!

Episode discussion

No comments, yet. Be the first!