ytlmike/laravel-model-creator

从命令行、迁移文件和JSON文件创建更易于维护的Eloquent模型。

v0.3.2 2023-01-28 07:52 UTC

This package is auto-updated.

Last update: 2024-09-28 11:33:47 UTC


README

自动生成更易于维护的Eloquent模型类(并创建迁移、工厂、种子等)。

开始

在您的Laravel项目中使用Composer安装laravel-model-creator

$ composer require ytlmike/laravel-model-creator

使用create:model命令创建模型类

$ 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):
 > 

生成的模型类

<?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;
    }
}

您可以使用--const选项生成字段常量:

$ php artisan create:model --const

生成的模型类

<?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;
    }
}