Search code examples
phplaravelvisual-studio-codeeloquent

How do I get Eloquent autocompletion in Visual Studio Code?


I am unable to get autocompletions for the methods for Eloquent models in Visual Studio Code.

enter image description here

Using DB instead of the model does show the methods.

enter image description here

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.


Solution

  • 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 the \Illuminate\Database\Eloquent\Model file on your project.

    Now, while the class does not have those methods, it could instantiate a \Illuminate\Database\Eloquent\Builder object which has all those methods. So every time you are doing Post::where(...), what actually happens is the Post class invokes the PHP's magic method __callStatic, which then invokes __call, which finally instantiates an Eloquent Builder object and call the where 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(...) because DB::table(...) returns a \Illuminate\Database\Query\Builder object, which has where 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