rossity/laravel-modelgenerator

该包已被放弃,不再维护。没有推荐替代包。

轻松构建API模型

1.1.9 2018-10-29 19:43 UTC

This package is not auto-updated.

Last update: 2020-08-22 07:25:43 UTC


README

轻松构建API模型。

这非常针对我自己的用例和项目。

命令

php artisan generate:template {模型}

  • 生成一个模板到App\Http\Templates,然后在下一条命令中调用

php artisan generate:model {模型}

  • 查找第一条命令中模型名称对应的模板文件

使用方法和示例

php artisan generate:template Comment

根据配置规范配置文件

 /**
  * The fields associated with the model
  *
  * @var array
  */
  public $fields = [
      'field_string' => [         // The name of the field
          'type' => 'string',     // In Migration and Request
          'default' => null,      // In Migration
          'required' => false,    // In Request
          'nullable' => true,     // In Migration and Request
          'extra_rules' => '',    // Validation rules other than 'nullable|required|type'
          'filterable' => true,   // Filterable via QueryBuilder, one of true|false|'exact'
      ],
  ];

  /**
   * Model that this model belongs to
   * Options below pertain only to the parent model
   * Used to create a relationship in the parent model and current model
   *
   * @var array
   */
  public $related = [
      'name' => 'Post
      'type' => 'hasMany' 
  ];

  /**
   * Log activity of this model using spatie/laravel-activitylog
   *
   * @var boolean
   */
  public $logActivity = true;

  /**
   * Add media to this model using spatie/laravel-medialibrary
   *
   * @var boolean
   */
  public $addMedia = false;

php artisan generate:model Comment

生成以下内容

- Model in `app\Http`
- Resource Controller in `app\Http\Controllers\Api`
- Policy in `App\Http\Policies` and registers it in `App\Providers\AuthServiceProvider`
- Resource and Collection in `app\Http\Resources`
- Request in `app\Http\Requests`
- Factory in `database\factories`
- Migration in `database\migrations`
- Appends a resource route to `routes\api.php` or adds it to apiResource array