تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Why IDE Helper Matters0:00

If you work with Laravel and with Eloquent, I think it's really important that you use this package called Laravel ID Helper. I love this package. I cannot work without it. As you probably know, Eloquent uses the active record pattern. And the way it works internally is it stores the model's attributes in an array. Internally, it is an array, and when you call a property, it uses a magic method to look in that array, and if a property exists, it returns to you. There's a little bit of magic, and what it causes is text editors cannot understand Laravel's magic. So if you have a model, for example, your text editor will probably not know

Install and Run Models0:33

text editors cannot understand Laravel's magic. So if you have a model, for example, your text editor will probably not know which properties that model holds. What this package does is it generates doc blocks for all of your models so that any text editor can understand it, whether it is PHPStorm, Sublime Text, VS Code, it doesn't really matter. It's really simple to install it, so let's copy this, go back to PHPStorm. I'm going to paste the command here. Let's wait for it to run. Perfect. Let's go back to the documentation and see how do we run it. This is the command we want. We're looking for models.

Perfect. Let's go back to the documentation and see how do we run it. This is the command we want. We're looking for models. So if we go back to PHPStorm and we run this, it is going to ask us whether we want to overwrite the existing model files. I will say no. If you say yes, it will add the doc blocks to the class, and it gets a little bit messy, so let's say no. And it will create this file called IDEHelperModels.php. Let's look for it. And as you can see, it just has doc blocks. So for the User model, for example, it has doc blocks for all of its properties. Now, as you can see, the problem is, well, we only have the User model here,

Fix Models Directory1:35

So for the User model, for example, it has doc blocks for all of its properties. Now, as you can see, the problem is, well, we only have the User model here, and we have more models. We have Product, we have Order, we have those. Thankfully, it is a small fix. By this point, I assume you already know what it is. We have to tell the package where to load things from. It is looking for models within the app directory or models do not live there. Thankfully, it is an easy fix. So I'm going to type php artisan ide-helper:models. This is the command we ran previously.

This is my shorthand command for php artisan. IDE Helper Models. This is the command we ran previously. Now, we just want to pass the dir option, and we can say that we want to look for models within the modules directory. Let's run this. Now, we do not want to overwrite files. Boom, there we go. So here we have our Order model, all of its properties, OrderLine model, all of its properties, CartItem, Product, Shipment. So every model that we have within the modules directory gets generated. My suggestion for this is you do not want to keep typing this all the time.

Add Composer Script2:42

So every model that we have within the modules directory gets generated. My suggestion for this is you do not want to keep typing this all the time and passing this option. I suggest that you go to composer.json. And if you scroll down a little bit, you have the scripts section where you can write composer scripts. So we can create one, for example, called IDE or something like that, or IDE models. I prefer short commands. Oh, thanks, Copilot. But this is not what we want. We want this. Let's add the @ sign.

Oh, thanks, Copilot. But this is not what we want. We want this. Let's add the at sign. We want to pass the directory like this, and we can save. So let's run this as a composer command. We can say composer run IDE. As you can see, it's writing the command. For example, I'm going to delete all of this. Let's regenerate it. Yes. And there we go. So it's generating using the dir option that we were passing to it. I think that's a good way to handle that.

When to Regenerate Docblocks3:36

And there we go. So it's generating using the dir option that we were passing to it. I think that's a good way to handle that. We don't want to keep passing. It's annoying to keep passing that option all the time. So just create it as a composer command or whatever you prefer, but just make sure that you have a way to quickly run it. And that way you can have your doc block for all of your models. Every time that you do a migration change or you change your table somehow, you want to run this. If we go back to our task, now our IDE is going to understand all of its attributes. It knows we have the name, created_at, ID, stock, etc.

If we go back to our task, now our IDE is going to understand all of its attributes. It knows we have the name, created_at, ID, stock, etc. Again, this is a must-have package for me. I cannot work with Alec when without this package. All right, as you see, pretty simple to configure. Again, I hope you all enjoyed this lesson, and I'll see you on the next one. Bye-bye.

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