Upon generating a new model in Laravel, a file like the following is created:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
//
}
This file does not make obvious the large number of protected members available. Nearly 100% of the time I find myself having to manually type:
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
just to get started. It would make development faster and prevent simple bugs from accidentally mistyping these common attributes if the Model stub gave you a pre-commented list of available protected members when a model was first generated. Something like:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class MyModel extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable;
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates;
// ... (continue with all protected members)
}
I feel this would make Laravel a bit easier and faster to work with, and it would prevent simple mistakes like misspelling a protected member.
php artisan make:model MyModel
What about the developers that already read the documentation and knows how it works and don't want to clutter their model with useless code?
Best way would be to introduce an option into the make model command to include these attributes if desired. Eg. php artisan make:model MyModel --attrubutes
php artisan make:model MyModel --attributes
I could agree with that.
If enough people agree I may go ahead and work on a PR for it. Shouldn't be too complicated.
If you need it for yourself, you can easily extend the MakeCommandConsole and override the stub method, but if the goal is to help new developers, then you're in a contradiction because they won't know about the optional parameter without reading the documentation and if they read the documentation, they don't need this at all.
open feature requests in the laravel/internals repo. :)
For suggestions and feature requests please use the https://github.com/laravel/internals repo.
Most helpful comment
Best way would be to introduce an option into the make model command to include these attributes if desired. Eg. php artisan make:model MyModel --attrubutes