4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[laralib/l5scaffold] make:scaffoldコマンドの使い方

Posted at

概要

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.

こちらも同様でした。残念。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?