概要
Laravel 5.x Scaffold Generator公式ドキュメント(README)のExamplesがあまりにも少なすぎるので、ソースを読むなどして分かった使い方をまとめます。
この記事で書かないこと
インストール方法についてはREADMEでご確認ください。
また、Laravel5.5以降ではコードの修正が必要なようです。詳細についてはLaravel5.6でscaffoldが動かなかった件が参考になるかと思います。
基本的な使い方
$ php artisan make:scaffold --help
Usage:
make:scaffold [options] [--] <name>
Arguments:
name The name of the model. (Ex: Post)
Options:
-s, --schema=SCHEMA Schema to generate scaffold files. (Ex: --schema="title:string")
-f, --form[=FORM] Use Illumintate/Html Form facade to generate input fields [default: false]
-p, --prefix[=PREFIX] Generate schema with prefix [default: false]
-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:
Create a scaffold with bootstrap 3
ヘルプに出てこないオプションもあるようなので、その辺もフォローしていきます。
schemaオプション
基本的な使い方は、マイグレーションファイルに$table->method1('column_name')->method2(arg)
と書くところを、column_name:method1:method2(arg)
と書いていく感じです。
カラムとカラムの間はカンマ,
で区切ります。
なお、id
, created_at
, updated_at
を明記する必要はありません。(デフォルトで作成されます)
では、試してみましょう。
$ php artisan make:scaffold --schema="title:string(40), published_at:dateTime:nullable:comment('公開日時')" Article
Configuring Article...
Migration created successfully
Seed created successfully.
Model created successfully.
Controller created successfully.
Layout created successfully.
Error created successfully.
Views created successfully.
Dump-autoload...
Route::resource("articles","ArticleController"); // Add this line in routes.php
作成されたマイグレーションファイルを一部抜粋します。
Schema::create('articles', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 40);
$table->dateTime('published_at')->nullable()->comment('公開日時');
$table->timestamps();
});
なるほど、どうやら...
- カラムタイムを指定するメソッド(stringなど)に2つ目以降の引数がある場合、それを渡すこともできる。
- 引数がない場合、
()
は省略可能 - メソッドはいくつでもチェインできる
ようですね!
これなら後は、Laravelドキュメントのカラム作成、カラム修飾子を参考にして自分の定義したいモデルが定義できそうです。
余談
validatorオプション
$ php artisan make:scaffold --schema="title:string, body:text" --validator="title:required|unique:posts|max:32, body:required" Article
[2018-11-03 13:09:19] local.ERROR: The "--validator" option does not exist. {"exception":"[object] (Symfony\\Component\\Console\\Exception\\RuntimeException(code: 0): The \"--validator\" option does not exist. at /var/www/vendor/symfony/console/Input/ArgvInput.php:223)"} []
The "--validator" option does not exist.
テストファイルに記述があったので試してみたのですが、エラーになりました。いずれ実装するつもりだったのでしょうか?(1年以上更新がない)
lang, localizationオプション
$ php artisan make:scaffold --schema="title:string, body:text" --lang=ja --localization="title, body" Article
[2018-11-03 13:19:15] local.ERROR: The "--lang" option does not exist. {"exception":"[object] (Symfony\\Component\\Console\\Exception\\RuntimeException(code: 0): The \"--lang\" option does not exist. at /var/www/vendor/symfony/console/Input/ArgvInput.php:223)"} []
The "--lang" option does not exist.
$ php artisan make:scaffold --schema="title:string, body:text" --localization="title, body" Article
[2018-11-03 13:19:28] local.ERROR: The "--localization" option does not exist. {"exception":"[object] (Symfony\\Component\\Console\\Exception\\RuntimeException(code: 0): The \"--localization\" option does not exist. at /var/www/vendor/symfony/console/Input/ArgvInput.php:223)"} []
The "--localization" option does not exist.
こちらも同様でした。残念。