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.
12 episodes1 hr 2 mins

Overview

Actions are single classes that do one thing. Laravel Actions is a package that allows you to run these classes as anything you want, whether it's a controller, listener, console command – or just on its own.

Keeping your app structure to single classes like this lets you focus on what your app does rather than the design decisions around controllers, listeners and commands.

Single actions are also easier to test, and we'll also cover that!

This course is for you if:

  • You'd like to try a fresh approach to structuring your apps
  • Your app shares logic, and you'd like to combine this logic into one class that runs anywhere
  • You've heard of (or used) Laravel Actions, and you'd like a run-through
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!