skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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 IlluminateDatabaseEloquentModel 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 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 IlluminateDatabaseQueryBuilder 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search