Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 694
RaV Avatar asked Jan 11 '18 16:01

RaV


2 Answers

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.

like image 82
RaV Avatar answered Oct 17 '22 19:10

RaV


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');
  }
}
like image 5
Eduardo Cuomo Avatar answered Oct 17 '22 21:10

Eduardo Cuomo