Playing
02. Creating your first action

Transcript

00:00
Okay so we're going to get started creating our first action. Let's just go through some of the things that we've already set up here. You can see that I've got a dashboard with your post here and the ability to enter something. This doesn't do anything at the moment, that's exactly what we're going to be filling in. We're going to start by creating out a controller which uses an action
00:22
inside of this. Now all of this I've set up with the Laravel Breed Starter Kit. I've already got my database set up if we just head over here and if you want to grab the markup for this form you can find these in the course resources. So you've got everything to get started if you want to follow along. Now we haven't got Laravel Actions installed at the moment so a good place to start
00:44
would be just coming over and installing this. Very very simple we just need one command and pretty much everywhere we're going to use this we'll be using this as action trait. So let's go ahead and just pull the command over here, get this installed and we are good to go. Okay so like I said we're going to be creating a controller to handle this first of all. So let's go ahead and
01:05
just create our controller as we normally would within an app if we weren't using actions. So let's go ahead and create a PostStoreController. We'll just make this a single invocable controller. So let's go ahead and open this up and we'll add an invoke magic method in here. Let's just die dump here just so we know that this works and let's go over to our root, register this and hook
01:28
up our form. Okay so just down here let's go ahead and create out a post route for this and of course we'll just say slash posts and we'll reference the PostStoreController in here and that's pretty much all we need to do. Okay so if we head over to the dashboard.blade.php file let's hook up our action here just to go through to posts and as you would imagine if I just fill this out very quickly
01:50
and hit create that dumps out what we see here. Great okay so it's time to create our first action. Now we already spoke in the introduction about what actions are but let's just dive into that a little bit more. So actions are single things within your app and this is just a new way of working in terms of creating small single things that just have one job. This makes things really
02:13
really easy to test. It makes things very easy to use. So really all you want to do here is just call an action and then maybe return back. So let's go ahead and create an action. Now once we've installed the Laravel actions package if we just go ahead and run php artisan and just run it on its own you'll see if we just head up here to our make section we actually have now the ability to make
02:41
an action. So that's been newly added with this package. So we're just going to leave these in the default section that they are created in. We're going to go ahead and say make action and what do we want to call this? Well let's think about what we're doing here. We are creating a post. We've obviously not set anything up here but let's just call this create post. So that's just a single
03:01
thing it will create a post for a user based on the data that we sent down to this. So let's go ahead and make that action out and let's first of all check out where these are placed. So if we just head over to our main app section you can see we've got this actions folder and this create post action is now inside of here. Now my editor is highlighting this red at the moment. I'm just
03:22
going to go ahead and re-index my workspace and you can see that gets rid of that and you can see here we have got a handle method. So let's just die dump in here instead now and just say action and let's see how we can call this action that we've just created. So we're going to do that from over here. So there's a few different ways that we could do this. The first way would be to
03:43
use create post and then go ahead and make this and that will give back us a create post object. Let's just die dump on that just so we can prove that this is working. If we go through here and just submit this form you can see we get an instance of create post. What we could then do is go ahead and handle this so we could even assign this if we wanted to and if we needed to
04:05
and if we go ahead and say action and handle you guessed it that'll go ahead and run that handle method that we have over here. So let's go ahead and just try this out first give this a refresh and there we go we get die dumped action out which we're doing inside of here. Now that's a little bit verbose and we're not always going to need to do that so another way
04:24
to do this is to go ahead and say create post and run. So let's go and just do this on its own without dumping anything out and what we should see if we run this exactly the same result. So I'm sure you'll agree this is a little bit more convenient but of course assigning actions can be useful too if you need to pass them around or do something else. So let's just add before we
04:47
actually start to make this work a little bit of validation. Now for the body of our message this is just required and I'm just going to switch the id over to body so let's just validate this in here and there's a good reason that I am going ahead and validating this within the controller and that is because we're going to be looking at running actions as a controller a little bit later.
05:07
So I'm going to bring the request object just inside of here and we're going to go ahead and pass in the request object to validate and we'll go ahead and just choose the rules for the body. For now let's just make this required and we'll just leave it at that. So if we head over to posts here or head over to the dashboard just here and we hit create we get the body field is required.
05:29
Great so that's working nicely. Okay so we're doing everything that we would normally do within an app we're validating our data we are doing something in this case it's going to create a post for the current user and we're returning back. So at the moment this seems a little bit overkill for what we're doing. Surely we would just add a single line of code in here maybe request
05:51
user and posts and create to create that post using maybe the validated data from this so we could just assign the validated data to this. So that is of course an option but we're going to do this and slightly refactor this to run this specific action as a controller a little bit later. So let's just go ahead and implement the functionality just so we've got it working
06:15
and then we'll go and cover all of that throughout the course. Okay so if we head over to create post we're going to fill this in with something a little bit more useful and look at how we can pass parameters to this. Now just a bit of setup first of all so I'm going to go ahead and make out a model in here this is going to be post we'll generate a migration and a factory alongside of
06:35
this as well because we are going to be covering testing and let's go over to the create posts table and just fill this in we'll just make this really really basic. So of course this is going to be attached to a user so let's use foreign id with a user id and we'll constrain that and of course a post needs a body so we'll just make this text and just say body that's pretty much all we need
06:57
for now. So let's go ahead and run php artists and migrate we'll set up the relationship and we'll get this created. So over in the user model let's come down here and just at the very bottom go and create out a posts relationship and we'll go ahead and return this has many and of course a user will have many posts a very very simple relationship that you're probably already used to. So how are
07:23
we going to build out this create post action well because a post is always going to belong to a user it's probably worth passing an actual user through as a parameter to this so we'll accept a user in and we can of course type in that and then we just want some data of course data might change between posts and where we actually invoke this action so let's go ahead and type in an array of data in
07:47
here that we can insert into that post so if we think about the example that we had before over on the post store controller where we said request user posts and then create we've pretty much within this action now got all of the data that we could possibly need so if we head over to the create post action we've got the user and we've got the data so really all we need to do in here is just
08:10
return the result of the user that we've passed in accessing the posts relationship and creating a post with the data that we've given now notice we've not got any validation in here at the moment so technically what we could do is just use this anywhere without having any kind of validated data but we will get onto that a little bit later what we can now do though is over in our post store
08:34
controller when we run this we can pass through the authenticated user and we can pass through the data that we need so in our case we could grab that from the validated data and let's just do that there we go so what we can now do is go ahead and actually create a post and see it appear here so let's come over here and just say post one create that out and at the moment yeah we should
08:56
just need to add body to the post model so let's go ahead and do that first of all or we could set this as unguarded and let's make sure that body's in there and we should be good let's give that a refresh do exactly the same thing we're redirected back and we now have a post inside of the database so that is your first action at the moment like i said seems a little bit pointless to bring in all
09:22
of this functionality just to pretty much replace this out with a single class in here although when we look at testing even doing this will make a little bit more sense okay so now we've covered creating our first action let's go over and look at how we can resolve an action from the container to potentially tidy this controller up a little bit if we were to go with this style
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!