zgldh/laracasts-generators
Composer 安装命令:
composer require zgldh/laracasts-generators
包简介
Extend Laravel 5's generators. For Laravel 5.4
关键字:
README 文档
README
This is just a fix for Laravel 5.4 cause the original package owner didn't do that.
If you're familiar with my Laravel 4 Generators, then this is basically the same thing - just upgraded for Laravel 5.
L5 includes a bunch of generators out of the box, so this package only needs to add a few things, like:
make:migration:schemamake:migration:pivotmake:seed
With one or two more to come.
Usage
Step 1: Install Through Composer
composer require laracasts/generators --dev
Step 2: Add the Service Provider
You'll only want to use these generators for local development, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php, like so:
public function register() { if ($this->app->environment() == 'local') { $this->app->register('Laracasts\Generators\GeneratorsServiceProvider'); } }
Step 3: Run Artisan!
You're all set. Run php artisan from the console, and you'll see the new commands in the make:* namespace section.
Examples
Migrations With Schema
php artisan make:migration:schema create_users_table --schema="username:string, email:string:unique"
Notice the format that we use, when declaring any applicable schema: a comma-separate list...
COLUMN_NAME:COLUMN_TYPE
So any of these will do:
username:string
body:text
age:integer
published_at:date
excerpt:text:nullable
email:string:unique:default('foo@example.com')
Using the schema from earlier...
--schema="username:string, email:string:unique"
...this will give you:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('email')->unique(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } }
When generating migrations with schema, the name of your migration (like, "create_users_table") matters. We use it to figure out what you're trying to accomplish. In this case, we began with the "create" keyword, which signals that we want to create a new table.
Alternatively, we can use the "remove" or "add" keywords, and the generated boilerplate will adapt, as needed. Let's create a migration to remove a column.
php artisan make:migration:schema remove_user_id_from_posts_table --schema="user_id:integer"
Now, notice that we're using the correct Schema methods.
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class RemoveUserIdFromPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function(Blueprint $table) { $table->dropColumn('user_id'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function(Blueprint $table) { $table->integer('user_id'); }); } }
Here's a few other examples of commands that you might write:
php artisan make:migration:schema create_posts_tablephp artisan make:migration:schema create_posts_table --schema="title:string, body:text, excerpt:string:nullable"php artisan make:migration:schema remove_excerpt_from_posts_table --schema="excerpt:string:nullable"
Now, when you create a migration, you typically want a model to go with it, right? By default, we'll go ahead and create an Eloquent model to go with your migration. This means, if you run, say:
php artisan make:migration:schema create_dogs_table --schema="name:string"
You'll get a migration, populated with the schema...but you'll also get an Eloquent model at app/Dog.php. Naturally, you can opt out of this by adding the --model=false flag/option.
Foreign Constraints
There's also a secret bit of sugar for when you need to generate foreign constraints. Imagine that you have a posts table, where each post belongs to a user. Let's try:
php artisan make:migration:schema create_posts_table --schema="user_id:integer:foreign, title:string, body:text"
Notice that "foreign" option (user_id:integer:foreign)? That's special. It signals that user_id` should receive a foreign constraint. Following conventions, this will give us:
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
As such, for that full command, our schema should look like so:
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->text('body');
$table->timestamps();
);
Neato.
Pivot Tables
So you need a migration to setup a pivot table in your database? Easy. We can scaffold the whole class with a single command.
php artisan make:migration:pivot tags posts
Here we pass, in any order, the names of the two tables that we need a joining/pivot table for. This will give you:
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostTagPivotTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('post_tag', function(Blueprint $table) { $table->integer('post_id')->unsigned()->index(); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); $table->integer('tag_id')->unsigned()->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('post_tag'); } }
Notice that the naming conventions are being followed here, regardless of what order you pass the table names.
Database Seeders
php artisan make:seed posts
This one is fairly basic. It just gives you a quick seeder class in the "database/seeds" folder.
<?php use Illuminate\Database\Seeder; // composer require laracasts/testdummy use Laracasts\TestDummy\Factory as TestDummy; class PostsTableSeeder extends Seeder { public function run() { // TestDummy::times(20)->create('App\Post'); } }
zgldh/laracasts-generators 适用场景与选型建议
zgldh/laracasts-generators 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 201 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「generators」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zgldh/laracasts-generators 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zgldh/laracasts-generators 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zgldh/laracasts-generators 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extend Laravel's generators scaffold.
Code generators for Zend Framework 2 in console.
Laravel Artisan Command To Generate Facades (php artisan make generator now execpts facade Facadename)
Advanced Laravel generators, that include schema information.
Generate service and repository file and interface
Alfabank REST API integration
统计信息
- 总下载量: 201
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-30