Intellisense (autocompletion) for model in Laravel for Visual Studio Code or another IDE
Asked Answered
P

3

10

I am looking for sth similiar to Intellisense in .NET in Laravel. So far I've been using .NET and autocompletion for models is awesome and makes my work way more easy.

Now I need to create Laravel application, I've been following this tutorial to prepare my environment but the only autocompletion I get is for default PHP functions and some predefined methods (ex. SaveSessionListener from Symfony\Component\HttpKernel\EventListener - I am not even using Symfony anywhere).

What I'd like to achieve is get autocompletion from models, for example there is a class called Model in Laravel, I have class Page which extends Model.

use App/Page

$home = new Page();
$home->content = "lorem ipsum";
$home->save();

I don't have any completion when I write $home->, no content, no save(), only some random functions. I can understand why there might be no content autocompletion - its not written directly into code, but its written on database and object-database engine is parsing that one, I didn't figure out how yet, but I don't understand why even save() doesn't get autocompletion.

I tried to google the issue, but without any good result.

Pinot answered 11/1, 2018 at 16:21 Comment(2)
take a look at laravel-ide-helper package github.com/barryvdh/laravel-ide-helperRajasthani
Seems a little bit complicated way, but I've look through docs and its promising. I'll test it later.Pinot
P
24

I figured it out how to get it work with Visual Studio Code.

First and most important is the link morph provided me in comment: laravel-ide-helper

I just followed docs and generated basic helper + model helper. I guess later I'll automate those generation commands, its also explained how to do it in docs.

Second thing is that it works only with: PHP Intelephense plugin Note that you need to reset VSC before it actually works.

Third thing I did - VSC have build-in php autocompletion, it was pretty annoying cause it started to show me model fields in suggestions, but it was between dozens of other suggestions. To disable build-in autocompletion I added line to user settings:

"php.suggest.basic": false,

Last thing I did - I have moved snippets to bottom of suggestions box to clear autocompletion results a little bit more:

"editor.snippetSuggestions": "bottom"

And that works really decent as Laravel programming environment.

Pinot answered 12/1, 2018 at 14:53 Comment(1)
Thanks a lot... it worked for me =) i have autocompletion now (laravel 5.8)Halvorson
R
5

I use PHP Doc for fields definition like following example:

namespace App\Models;

use App\Enums\MediaType;
use App\Models\Commons\BaseModel;
use DateTime;

/**
 * Famous Media.
 *
 * @property int $id
 * @property int $famous_id
 * @property MediaType $type
 * @property string $url
 * @property int $position
 * @property DateTime $created_at
 * @property DateTime $updated_at
 */
class FamousMedia extends BaseModel
{
  const TABLE = 'famous_medias';

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'type',
    'url',
    'position',
    'famous_id',
  ];

  /**
   * The attributes that should be hidden for arrays.
   *
   * @var array
   */
  protected $hidden = [
    'famous_id',
    'deleted_at',
    'created_at',
    'updated_at',
  ];

  public function famous()
  {
    return $this->hasOne(Famous::class, 'famous_id');
  }
}
Recommendation answered 28/9, 2020 at 15:23 Comment(0)
D
1

Update:

You can now use Laravel Extra Intellisense is now supporting Model Attributes.

Still in Beta but it works fine.

Donald answered 13/5, 2022 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.