Eloquent ORM Code Hinting in PhpStorm

后端 未结 6 1540
再見小時候
再見小時候 2020-12-08 00:57

So I\'m just starting off with Laravel (using v5) and Eloquent. I\'m working on getting some basic APIs up and running and noticing that a lot of working methods don\'t show

6条回答
  •  执笔经年
    2020-12-08 01:33

    For future Googlers, and perhaps OP as well if you are still sticking to Laravel.

    The laravel-ide-helper package solves this issue for you quite elegantly, with what I believe is a relatively new feature; generated model PHPDocs.

    You can generate a separate file for all PHPDocs with this command:

    php artisan ide-helper:models
    

    The generated metadata will look something like this for each class:

    namespace App {
    /**
     * App\Post
     *
     * @property integer $id
     * @property integer $author_id
     * @property string $title
     * @property string $text
     * @property \Carbon\Carbon $created_at
     * @property \Carbon\Carbon $updated_at
     * @property-read \User $author
     * @property-read \Illuminate\Database\Eloquent\Collection|\Comment[] $comments
     */
    class Post {}
    }
    

    This caused issues for me in PHPStorm however, where the software was complaining about multiple class definitions. Luckily an option is readily available for writing directly to the model files:

    php artisan ide-helper:models -W
    

    There are a few more options and settings available if you need to tweak the behavior, but this is the gist of it.

提交回复
热议问题