I am unable to get autocompletions for the methods for Eloquent models in Visual Studio Code.
Using DB instead of the model does show the methods.
I have the Post model imported, and I tried installing the IDE helper from barryvdh, but that didn’t help either. I have also tried reinstalling Intelephense and restarting VSC, no luck either.
2
Answers
You can get this kind of completions with the help of various extentions, just look up extension packs for your stack in VSCODE extension market.
Also consider adding PHPDoc block for each model,it helps a lot when it comes to that.
I also experience this and so far I haven’t found an extension that’s able to do auto-complete on Laravel model’s static call like this.
The reason being some Laravel’s Eloquent Model static methods such as
where
,find
, etc. are dynamically called, i.e. the model class itself does not have those methods. You could check for this yourself on theIlluminateDatabaseEloquentModel
file on your project.Now, while the class does not have those methods, it could instantiate a
IlluminateDatabaseEloquentBuilder
object which has all those methods. So every time you are doingPost::where(...)
, what actually happens is thePost
class invokes the PHP’s magic method__callStatic
, which then invokes__call
, which finally instantiates an Eloquent Builder object and call thewhere
method on that object.Basically, a common auto-complete VSCode extension which is usually based on the method list written in a PHP class file would never have an idea about Laravel Model’s methods like
where
,find
, etc. since those are dynamically called.The auto-complete works on
DB::table(...)->where(...)
becauseDB::table(...)
returns aIlluminateDatabaseQueryBuilder
object, which haswhere
method listed in its class file.TL;DR
So I guess for the time being, unless there’s such a VSC extension that I don’t know about, you should settle with typing
Post::where()
without auto-completes