laravel-model-creator maintained by ytlmike
laravel-model-creator
generate a more maintainable eloquent model class automaticly (and create migration, factory, seeds, etc.).
start
Install laravel-model-creator using composer in your laravel project:
$ composer require ytlmike/laravel-model-creator
use create:model command to create a model class:
$ php artisan create:model
Class name of the model to create:
> user
Class \App\Models\User created successfully.
New field name (press <return> to stop adding fields):
> name
Field type::
[0] int
[1] tinyint
[2] varchar
[3] datetime
> 2
Field display length [255]:
> 45
Can this field be null in the database (nullable) (yes/no) [no]:
>
Default value of this field in tht database []:
>
Comment of this field in the database []:
> user name
Add another property? Enter the property name (or press <return> to stop adding fields):
>
The generated model class:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
}
You can use --const option to generate field constants:
$ php artisan create:model --const
The generated model class:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* user name
* @Column (type='varchar', length=45, not null)
*/
const FIELD_NAME = 'name';
public function getName()
{
return $this->getAttribute(self::FIELD_NAME);
}
public function setName($name)
{
$this->setAttribute(self::FIELD_NAME, $name);
return $this;
}
}