2

I want my Code Completion (PhpStorm) to work with Eloquent models but if I declare fields corresponding to DB columns they no longer work:

class Limit extends Model
{
    public $id;
    public $type;
    public $limit;
}

Is there some workaround?

Poma
  • 8,174
  • 18
  • 82
  • 144

1 Answers1

1

(This is a slightly modified version of my own reply from this thread: Eloquent ORM Code Hinting in PhpStorm)

The laravel-ide-helper package can be used to help with this issue, by generating PHPDocs for your models.

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.

Community
  • 1
  • 1
Erik Johansson
  • 1,646
  • 1
  • 14
  • 19