در حال بارگذاری ...

Speeding Up Model Setup0:00

Alright, next up, why don't we talk about speed, and how using the Laravel IDEA plugin, we can take common tasks and execute them in a flash. I'll show you what I mean. Alright, so once again, I will create a Post model. So on a Mac, Shift-Command-C, create a model, and I'll call it Post. A Post consists of a title, and a body, and then also a User who wrote the Post. And that'll be a big integer. Alright, that looks good. So now, imagine the Eloquent relationship between a Post and a User. And of course, that relationship is a belongsTo relationship.

So now, imagine the Eloquent relationship between a Post and a User. And of course, that relationship is a belongsTo relationship. So of course, traditionally, we could write this out, User, return this belongsTo User. It's a lot of writing, isn't it? And then I can also add the return type, which is a belongsTo. And yeah, that works. It's fine. But again, it takes a little while. So here are some ways that we can shorten that. So let's bring it back to what I had before.

Using Live Templates1:00

So here are some ways that we can shorten that. So let's bring it back to what I had before. And yeah, one obvious thing we can do is reach for a text snippet. Or in PHPStorm, we'd call them live templates. So out of the box, I can use PubF, and notice it pops up right there. Or alternatively, I can press Command-J, and this will bring up all of the snippets I can reach for. Alright, now, once again, I can hit user, and then tab or return between the edit points. Enter, enter.

Omitting $this Calls1:26

and then tab or return between the edit points. Enter, enter. Yeah, and then once again, I can return that relationship. Okay, a little bit better, but we can keep going. Now notice, whenever I call a method, I have to type $this, arrow, and then the method name. However, you can omit the $this part, and PHPStorm will take care of it for you. So if I want to call a belongsTo method, I can just say belongsTo, and notice it instantly pops up. When I hit return, notice PHPStorm adds the current instance. Alright, so if instead this were a hasMany relationship,

Laravel Idea Relationships1:56

When I hit return, notice PHPStorm adds the current instance. Alright, so if instead this were a hasMany relationship, I could say return hasMany. And yeah, it's just a little bit quicker, but it really does add up over time. Okay, so better, but we can still take this a little further using the Laravel Idea plugin. So if I want to add a belongsTo relationship to an Eloquent model, have a look. All I have to do, as it turns out, is type the relationship name, and I see these two snippets or templates pop up.

All I have to do, as it turns out, is type the relationship name, and I see these two snippets or templates pop up. One is for a generic belongsTo reference, but this second one is really cool. If I hit return, alright, come on, that's really neat. And think about it. The plugin is smart enough to know that there is a user_id for the Post migration, which means there will be a corresponding relationship, and that's very, very cool. Alright, let's do another one.

Adding Comments Model2:46

and that's very, very cool. Alright, let's do another one. A Post consists of Comments, right? So let's get into the workflow. Shift-Command-C, make me a new model for a Comment. A Comment belongs to a User, a Comment has a body, and then also a Comment belongs to a Post. So post_id is also a big integer. Alright, let's give that a run.

Generating Model Relationships3:07

So postId is also a big integer. Alright, let's give that a run. Alright, there we go. And yeah, we should probably update this to add the necessary foreign key constraints, but again, this will take us 95% of the way there. Okay, let's update our relationships. So back to Post, the relationship is hasMany, right? A Post has many Comments. So I type the relationship name, and once again, the plugin is smart enough to know.

So I type the relationship name, and once again, the plugin is smart enough to know that we're probably looking for Comments. Very cool. Now, let's do the same thing, but in reverse. So I will Command-Click on the Comment model, and a Comment belongs to both a User as well as a Post. Alright, here we go. Belongs to a User, and belongs to a Post.

Belongs to a User, and belongs to a Post. I mean, come on. How cool is that? Click, click, click.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟