ignasbernotas / laravel-model-generator
1.2.3
2017-04-06 09:29 UTC
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
README
我非常抱歉地宣布,我不再有时间维护这个包。这个项目最初是在我需要将现有项目迁移到 Laravel 的时候,在几天内创建的。尽管它正在被积极使用(超过 50k 安装!),但我无法找到时间跟踪 PR 和可能破坏新旧 Laravel 版本中的某些变化,而且我在初始发布后也没有使用过它。代码库一团糟,迫切需要重写。
请使用 reliese/laravel 包。
模型生成器
Laravel 5 模型生成器,用于现有模式。
它连接到您现有的数据库,并根据现有表生成模型类文件。
安装
composer require ignasbernotas/laravel-model-generator --dev
您只希望将这些生成器用于本地开发,因此您不希望更新 config/app.php
中的生产提供者数组。相反,在 app/Providers/AppServiceProvider.php
中添加提供者,如下所示
<?php public function register() { if ($this->app->environment() == 'local') { $this->app->register('Iber\Generator\ModelGeneratorProvider'); } }
帮助 & 选项
php artisan help make:models Usage: make:models [options] Options: --tables[=TABLES] Comma separated table names to generate --prefix[=PREFIX] Table Prefix [default: DB::getTablePrefix()] --dir[=DIR] Model directory [default: "Models/"] --extends[=EXTENDS] Parent class [default: "Illuminate\Database\Eloquent\Model"] --fillable[=FILLABLE] Rules for $fillable array columns [default: ""] --guarded[=GUARDED] Rules for $guarded array columns [default: "ends:_guarded"] --timestamps[=TIMESTAMPS] Rules for $timestamps columns [default: "ends:_at"] -i, --ignore[=IGNORE] Ignores the tables you define, separated with , -f, --force[=FORCE] Force override [default: false] -s, --ignoresystem If you want to ignore system tables. Just type --ignoresystem or -s -m, --getset Defines if you want to generate set and get methods -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question --env[=ENV] The environment the command should run under. -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: Build models from existing schema.
运行生成器
php artisan make:models
示例
表 users
SQL
CREATE TABLE `users` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(64) NULL DEFAULT NULL, `password` VARCHAR(45) NULL DEFAULT NULL, `email` VARCHAR(45) NULL DEFAULT NULL, `name` VARCHAR(45) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB;
生成的模型类 Users.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Users extends Model { public $timestamps = false; protected $table = 'users'; protected $fillable = ['username', 'email', 'name']; protected $guarded = ['id', 'password']; }
表 posts
SQL
CREATE TABLE `posts` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `title` VARCHAR(64) UNSIGNED NOT NULL DEFAULT '', `content` TEXT NOT NULL DEFAULT '', `created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB;
生成的模型类 Posts.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Posts extends Model { public $timestamps = true; protected $table = 'posts'; protected $fillable = ['title', 'content', 'created_at', 'updated_at']; protected $guarded = ['id']; }