Generating Helper Code0:00
Okay. Hi, everybody. So, to start off this new section, I have compiled a list of five Laravel Idea-specific features that I think you need to know about. So, why don't we just jump in? I have my notes here, so I don't forget. All right, let's go. Number one. From time to time, you will want to visit the Laravel menu and choose Generate Helper Code. All right, so what this is going to do is generate background code that makes some of Laravel's more magical features understandable to PHPStorm. And here's what I mean. Let's go into our routes file. And maybe right here, we want to return some JSON that represents all posts that were created by a specific User. So, you might do something like this. User, just grab the first one, and then give me the post relationship. Right off the bat, we have a little squiggly line here, and we have property accessed via magic method. Okay. So, instantly,
grab the first one, and then give me the post relationship. Right off the bat, we have a little squiggly line here, and we have property accessed via magic method. Okay. So, instantly, it's letting us know there's something going on here. Let's have a look. I will command click on User, and if we scroll down, yeah, there is no post relationship. So, let's do that now. And as you learned many episodes ago, if a User has many Posts, I could just type hasMany, and you'll see there's an option to autocomplete that specific relationship. Okay, cool. So, let's hit command open brace to go back, and it still doesn't work, does it? And that would make sense. You'll see it can find the User record, and we could call a post method on it, but as we know with Eloquent, we can also call posts as a magic property that will fetch me the corresponding collection of Posts. And right now, in PhpStorm,
call a post method on it, but as we know with Eloquent, we can also call posts as a magic property that will fetch me the corresponding collection of posts. And right now, in PhpStorm, doesn't know anything about that. Okay. So, let's give it a shot. Right now, we have a squiggly line, but if I come up to Laravel and choose generate helper code, notice it disappears. And further, if I command click, it is now smart enough to take us to the appropriate relationship, even though we're technically accessing it as a magic property. So, now, yeah, I want you to take that little convenience and then multiply it across the entire code base. And that is what generating helper code does for you. All right. Number one, let's move on to number two. And I call this one autocompletion anywhere. I'll show you an example. Let's return to the routes file, and we will give a name to this route. It's
Autocompletion Anywhere2:24
move on to number two. And I call this one autocompletion anywhere. I'll show you an example. Let's return to the routes file, and we will give a name to this route. It's the homepage, so we often give this a name of home, right? Okay. Well, now, because we've assigned a route name here, we can reference that name from anywhere in the application. We'll just do it down here for convenience. Give me a route, and then I'll hit control space for my autocompletion, and sure enough, home pops up. And, yeah, this is going to be referenced anywhere. So, maybe within a controller, I could redirect to a route, and once again, control space, and it will provide instant autocompletion for my Laravel routes. So cool. Next, we have the same luxury when generating our Eloquent queries. So, for example, I could say User where, and once again, notice if I control space here, it's going to show
So cool. Next, we have the same luxury when generating our Eloquent queries. So, for example, I could say user where, and once again, notice if I Control Space here, it's going to show me all of the fields specifically for my User model. So, maybe if I want to find a User according to their email address, I could do that here, and then the value, of course, would probably come from the request or a form. I'll just hard code it. And we again have autocompletion. Why don't we go with firstOrFail? All right, and we'll save that to user. All right, next, this isn't uniquely related to the plugin, but it's a good one to know. This namespace is getting a little long for my taste, so I will select it. I will hit Option Return and choose simplify fully qualified class name, and now it imports it and replaces it with just the name only, and I think that looks a lot better. All right,
will hit option return and choose simplify fully qualified class name, and now it imports it and replaces it with just the name only, and I think that looks a lot better. All right, let's keep going. Let's go back to User, and maybe we've decided that we track down a User by their email address often enough to the point that it should either be a query scope or even a static method. Why don't we do a scope? So, I could call this scope and maybe byEmail, or how about withEmail? This will accept the query as well as the corresponding value. So, then I could say query->where, and once again, I have autocompletion, and then I can pass through the value. All right, let's try to use it. Now, if I come back to my routes file, and now let's replace all of this with our query scope withEmail, but I don't have autocompletion, and that makes sense. We haven't regenerated the helper code, so that's why
file, and now let's replace all of this with our query scope with email, but I don't have autocompletion, and that makes sense. We haven't regenerated the helper code, so that's why I said you want to get in the habit of doing this, or you can do it automatically. Generate helper code, and now if we try it again, notice the editor is smart enough to track it down with email, foo.example.com, and it'll just work. All right, now let's do a couple more, and then we'll move on to item number three. We also have autocompletion for things like configuration values. So, let's say I want to grab, I don't know, config('services'), and maybe the mailgun endpoints. Maybe we need to track that down. Okay, let's see if we can do it. So, if I type config('services'), once again, we have instant autocompletion, and I can see right here, this is the one I need. Or, because I know it's called endpoint,
can do it. So, if I type config/services, once again, we have instant autocompletion, and I can see right here, this is the one I need. Or, because I know it's called endpoint, I could even skip the file name and just type endpoint, and this will return all matching keys. So, in our case, it happens to be the one at the top, and we're all set. This one is especially useful, I think. All right, let's do one more before moving on to number three. Maybe I want to listen for a POST request to /posts, and this will insert a new Post into the database. But, of course, first, I must validate the request. So, maybe a title is required, and again, notice if I control space, it will autocomplete all validation rules. So, if I write required, there we go. And, yeah, this is going to work with the pipe syntax. So, maybe there's a minimum number of characters of, you know, 10, or whatever.
Using Built-In Generators6:53
about it a number of episodes ago, but I'm covering it two times because it's so incredibly helpful. So, again, if I come up to Laravel, we have code generation at the top, or you can review a full list of all of the generators here. So, I could generate an event, or a function, or a job, or a middleware, or a policy. I'm not doing the whole list, but you get the idea. Pretty much anything in a Laravel app that you need to generate is available, including things like tests. Give me a unit test, or a feature test. All of that is available. So, yeah, if I want a feature test, I could choose it here, or, again, in my own workflow, I wouldn't do this. I would use the shortcut, which is shift command comma. And then, again, I'm not going to fully write it out, create feature test. I would just write test, and it will track it down. All right. So, this is a test. While we're working
And then, again, I'm not going to fully write it out, create feature test. I would just write test, and it will track it down. All right. So, this is a test. While we're working with posts, why don't we say fetch all posts, or something like that, and it will instantly generate a PEST specific feature test for me. Now, a little note here. Maybe, if we do it again, feature test, maybe you want your test nested within a parent directory. So, in the last episode, we were talking about achievements. So, if I want this test nested within a parent directory, I just include the directory name as part of it. So, maybe something like award achievement test, give that a run, and now, once again, you can see it is in feature, achievements, award achievement test, and that's what I want. But on top of that, don't forget, once again, shift command comma, if I generate a model specifically,
Creating Blade Components9:04
a migration, and a factory, and a corresponding controller, and we'll click go. And there we go. It populated the migration. It gave us our model. I can select here to choose the related files. In this case, you can see the corresponding factory that it also created, and then, once again, it created corresponding fake data to go along with it. So neat. All right. Number four. And I've already forgotten it. What is it? Number four. Ah, Blade components. Yeah, this is pretty neat. Check this out. If we're sticking with posts, and I'm going to hide this sidebar here, I will hit shift, command, comma, to generate a Blade component, and what should we call this? Maybe blogPost? And let's give that a run. Okay. So, now we have a starter component that will be located in views/components/blogPost. So, yeah, here, maybe this would be an article where you would have a corresponding post or something.
have a starter component that will be located in views/components/blog post. So, yeah, here, maybe this would be an article where you would have a corresponding Post or something like that. Ard, coded, post, title. You get the idea. Okay. Now, let's reference this from one of our views. So, I will go into my routes file, and, yeah, maybe let's get rid of all of this. Why don't we go with route view in this case? So, if the user visits post, I will load the view in the post folder called index. But, yeah, notice I didn't get auto-completion because this view doesn't yet exist. However, I could hit option, return, and now create the view, and, yeah, it's just magical. It'll work exactly the way you expect, and it knows to place the view within the post directory. Okay. So, right here, I'm just going to use a full layout, and then I want to reference our new Blade component.
and it knows to place the view within the post directory. Okay. So, right here, I'm just going to use a full layout, and then I want to reference our new Blade component. So, notice if I start typing the tag for our component, again, we have auto-completion here, which is pretty cool. Okay. But now, of course, we might need to pass the title to this component. So, I will command click to instantly visit that component, which is cool. And, yeah, here, we will replace this with the title. All right. Let's declare at the top that we have props, and, again, we have auto-completion here for the title. Okay. Let's switch back and try to pass it through. So, once again, x-log-post, and notice if I start typing, again, we have Blade component prop auto-completion. Pretty neat. And, yeah, this would be something like that, or I'll just use a string. Pretty cool. Now, on that
Autocompleting Fillable Fields11:23
I start typing, again, we have Blade component prop auto-completion. Pretty neat. And, yeah, this would be something like that, or I'll just use a string. Pretty cool. Now, on that note, though, before we move on to the next item, let's say that you haven't yet created the Blade component. And, instead, you have it nested maybe within a foreach loop or something like that. Okay. Well, what you could do is select the corresponding HTML, and then open the refactoring menu with Ctrl + T, and, yeah, notice there's an option to extract a Blade component. So, exact same process. It extracts the file into the components directory, and we get the exact same thing. Very cool. Okay. So, number five, and this will be our last one, we have auto-completion for fillable fields. So, let's go into my Post model, and, yeah, if I want to be explicit about my fillable fields, I could start typing
will be our last one, we have auto-completion for fillable fields. So, let's go into my Post model, and, yeah, if I want to be explicit about my fillable fields, I could start typing protected $fillable, but, again, just phpStorm in general doesn't require this. I can, instead, start typing the property name, and it will figure out that, oh, this is probably what you're looking for. So, yeah, that will save a lot of typing. Okay. So, now, what are the fillable fields? And, yeah, traditionally, if you're using sublime text or something like that, you can often forget. So, you'll have to go back to either your database table or your migration to figure out, yeah, what did I have here? A title, a body, and a user_id. Okay. Notice, though, if I hit control space, it will give me auto-completion, again, for those attributes. So, yeah, we could go with body and whatever else might be appropriate.
ID. Okay. Notice, though, if I hit Control Space, it will give me auto-completion, again, for those attributes. So, yeah, we could go with body and whatever else might be appropriate there. Pretty neat. And, yeah, the same is going to be true if I select the class name and hit Command N. What if we generate, how about, a JSON resource for a Post? Notice, again, it will auto-complete the corresponding attributes, and then I can reformat, tweak it however I need to, and I'm off to the races. And, yeah, trust me, what's going to happen is you will take this for granted very quickly, and you'll forget that before PHPStorm and before the plugin, you were manually writing out this code for every single resource in your application, for every single fillable field, for every single config item or route name. So, trust me, your fingers are going to be thankful for this plugin. Okay. So,
