Introducing code generation0:00
All right, next up, why don't we have a look at code generation? Okay, so for an example, imagine that you need to generate, say, an Eloquent model. All right, well, traditionally, in a different editor, you might switch to your terminal. So you could do it like this, php artisan make:model. And how about a model for, I don't know, an activity feed. So we will call it Activity. All right, and that looks good. Now, of course, I'd open up app/models/Activity.php, and we're off to the races. And yeah, this is sort of the traditional approach. It works great.
Using PhpStorm terminal0:36
And yeah, this is sort of the traditional approach. It works great. Now, first, keep in mind that phpstorm does have a built-in terminal that you can reach for. You can turn it on in a few ways. Go to view, appearance, and we could enable tool window bars. And yeah, now I can toggle many things. And right down here at the bottom is the terminal. And that works great. Another option, and actually, real quick, let's turn that off. Another option would be to manually select view, tool windows, and then come down to terminal.
Generating via Laravel plugin1:27
Just hit command comma, go to your key map section, and then you can bind it here. All right, very cool. So, anyways, this is fine, and it's very much a traditional workflow. However, let's delete this. I'll hit delete. I will select the current directory, press command N, and yeah, have a look right down here. We have this Laravel menu for Laravel-specific file templates or generators. But now, just keep in mind, if you're not seeing this on your machine, of course, go to your settings, visit the plugins area, and make sure that you have the Laravel plugin installed.
of course, go to your settings, visit the plugins area, and make sure that you have the Laravel plugin installed. Otherwise, it won't work. Okay, anyways, let's do it again. Command N. This time, I'll just start typing to filter the results. I'll hit enter, and let's generate a model. Okay, we called it Activity, right? And notice, by default, it has all of these selected, which means when I run this command, it'll create the model,
And notice, by default, it has all of these selected, which means when I run this command, it'll create the model, but also a migration, also a factory, also a JSON resource, all the things that you might be used to when working with Laravel. But yeah, if you don't want that, just disable these, and this would give you a simple Activity model, as you can see here. All right, so we have a little pop-up. Do we want to add this to Git? No, and don't ask again. Great. Also notice, when we run that command, behind the scenes,
Great. Also notice, when we run that command, behind the scenes, the plugin will automatically update its helper code, which is great. Okay, so let's do this workflow again. So, of course, you can manually select the directory, but like I discussed, I will often keep that sidebar hidden. Instead, I could either use the floating menu by pressing command + up, and yeah, I could just find it in this way, app, models, and then command + N with that directory created, or there is a plugin-specific generator that we can reach for.
app, models, and then command N with that directory created, or there is a plugin-specific generator that we can reach for. Let's go to the top, and you'll see a Laravel menu, and yeah, we'll select code generation, or again, notice there is a shortcut, shift, command, comma. So let's try that now, shift, command, comma, and yeah, if I type create, you'll see a massive list of all the various Laravel things that we can generate. So yeah, if I want to model, I could type create model, or really, just model would be fine.
Model generation with fields3:31
So yeah, if I want to model, I could type php artisan make:model, or really, just model would be fine. All right, so once again, the workflow would be on my machine, shift, command, comma, model, return, Activity, and this time, why don't we populate the fields, and this is especially useful if we also want to have it take care of our migration, and then why don't we also ask it for a factory, a controller, and a form request. Okay, so what might we need for Activity? Well, probably a userId, and that would be a unsignedBigInteger.
Okay, so what might we need for activity? Well, probably a userId, and that would be a unsignedBigInteger. I can hit tab and space to add more, so that way I don't have to use the mouse if I don't want to, and who knows, maybe activity has, I don't know, a body or a message. Why don't we stick with message, and that can even be text if we need to, and yeah, that's fine for now. So let's go ahead and run this, and there we go.
and yeah, that's fine for now. So let's go ahead and run this, and there we go. So now, yeah, think about it. It took only a few seconds to run a command that generates our Activity model. It creates the migration file. It populates the schema for a migration file, and yeah, once again, if I go to the terminal and I run git status, here's all of the other things it created.
and I run git status, here's all of the other things it created. So it added a controller, an ActivityRequest, a form request, the Activity model, a database factory, and the migration, and yeah, you'll even notice if I go into ActivityController, it will populate this, and again, you can delete this if you want, but it'll give you a basic idea of what you probably want. So in this case, we have a standard resource.
but it'll give you a basic idea of what you probably want. So in this case, we have a standard resource for displaying Activitys. So if I add a new Activity, that will accept a form request, and notice again, it will populate that for you. And yeah, like I said, this is incredibly helpful stuff. Trust me, you're going to reach for it all the time. And then also keep in mind, if I open up, say, my model, well, have a look at this little Laravel icon here.
Navigating related model files5:16
And then also keep in mind, if I open up, say, my model, well, have a look at this little Laravel icon here. If I select it, now it'll show me all related files for this model. So in this case, it would be the database factory as well as the migration, and that can be really helpful. All right, let's do another one. Let's use the obligatory... Let's close these out. The obligatory Post model.
Let's close these out. The obligatory Post model. All right, Shift, Command, Comma. Let's generate a model for a Post, tab, tab. A Post consists of a title. Let's space that, another one. How about a body, and that will be text. And then a Post also belongs to an owner. So why don't we call that user_id, and that will be a bigInteger.
So why don't we call that userId, and that will be a bigInteger. Okay, why don't we also create... In this case, let's say I don't need a form request, but just the migration, factory, and controller. All right, there we go. So here's our migration. If I want, I could bring back the terminal and run php artisan migrate, and that'll whip it up on the fly.
and run php artisan migrate, and that'll whip it up on the fly. All right, let's go to the Post model, Command P, post. And that looks good. Why don't we switch to the factory, and we can review and tweak it however we need to. So in this case, why don't we make userId equal to a User factory, so that the relationship points to an actual User in our system that we generate.
Standard PHP class generation6:36
so that the relationship points to an actual User in our system that we generate. Finally, let's visit the PostController, and yeah, you'll see it will automatically populate something resembling what you would probably want in real life. All right, and finally, let's talk about some standard file generation. So within the app directory, Command N, let's create a simple PHP class, and we'll call it Project.
let's create a simple php class, and we'll call it Project. And let's do this. I will first hide the sidebar with Command 1, and let's say a Project needs to be instantiated with a title. Okay, I could say protected string $title, and now notice if within this class or within the curly braces, if I hit Command N, I can generate the constructor dynamically.
where it lets you know, hey, this property is currently dynamic, and you may not want that. Once again, select it, Option, Return, and I can manually add that property. All right, but in this case, it's set to private. Maybe I want it to be protected. Once again, Option, Return, and I can switch that to protected or public. Or just to keep having fun,
and I can switch that to protected or public. Or just to keep having fun, at least what I consider fun, if we are using PHP 8 or higher, I could alternatively select here, Option, Return, and I can switch this or convert it to promoted properties, and now we can do it all in line there. All right, let's save, reformat, and that looks good to me.
All right, let's save, reformat, and that looks good to me. Okay, next, we can also generate getters and setters. Now, just keep in mind, in the latest version of PHP, you might be able to get away with something like that by declaring it read-only. But yeah, otherwise, if you want a traditional getter or setter, it's the exact same process. Command-N, and let's create a getter, and I only have a title here, so that's all I see.
Command-N, and let's create a getter, and I only have a title here, so that's all I see. Let's generate that, and there we go. Let's now do it again for a setter, but this time, I won't touch the mouse. All right, Command-N, I will type to start filtering in, Return, Return again, and now we have a setter and a getter. Or, of course, you can do them at the exact same time.
and now we have a setter and a getter. Or, of course, you can do them at the exact same time. So, Command-N, and there's an option for getters and setters. All right, and I think that's going to do it. So, what did we learn in this video? Well, two things. First up, we learned that, as always, Command-N is your friend. Just position the cursor, hit Command-N,
Command-N is your friend. Just position the cursor, hit Command-N, and have a look at all the generators that pop up. But second, because we have the Laravel plug-in installed, we have a wide array of file generators that we can reach for. And then further, in situations where you're generating a model, yeah, this is going to take you 98% of the way. So, it's pretty cool, and I promise, you're going to reach for it constantly.
