2
1

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.

EC2のLaravel6.0環境 VueとLaravelでのTIPS AWS/Laravel連載(10.5)

Posted at

LaravelのシーディングにFakerを入れる

連載9回目で初期値でhoge, fugaという値を入れました。
実際テストする中だといちいちダミーの名前やタイトル、文章等を考えるのは一苦労です。

そこで便利なのがFaker。
Laravelだと最初からcomposer.jsonに入っているので、install不要です。

そしてシーディングだけでなく、Laravelのfactoryという仕組みを作って汎用性のある書き方をしてみます。

$ php artisan make:factory PostFactory --model=Post
database/factories/PostFactory.php
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
    return [
        'title'   => $faker->sentence,
        'content' => $faker->paragraph,
    ];
});
database/seeds/PostsTableSeeder.php
...
     public function run()
     {
        $posts = factory(App\Post::class, 3)->create();
     }
$ php artisan db:seed

これで3件分のダミーデータが生成されます。

php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.22  cli) by Justin Hileman
>>> Post::all();
[!] Aliasing 'Post' to 'App\Post' for this Tinker session.
=> Illuminate\Database\Eloquent\Collection {#3084
     all: [
       App\Post {#3085
         id: 1,
         title: "hoge",
         content: "fuga",
         created_at: null,
         updated_at: null,
       },
       App\Post {#3086
         id: 2,
         title: "Repellendus odit qui facilis ea sint.",
         content: "Est eos sed amet quibusdam. Et voluptatem voluptatem et accusantium qui.",
         created_at: "2019-10-12 21:05:10",
         updated_at: "2019-10-12 21:05:10",
       },
       App\Post {#3087
         id: 3,
         title: "Neque rerum ut molestiae aut sequi.",
         content: "Rem exercitationem doloremque facilis et sed qui blanditiis. Dolorem ea quo in voluptatem. Distinctio tempora dolor culpa reprehenderit fuga voluptas omnis. Animi voluptas repudiandae rem quisquam eligendi dolores. Quibusdam iste aut possimus.",
         created_at: "2019-10-12 21:05:10",
         updated_at: "2019-10-12 21:05:10",
       },
       App\Post {#3088
         id: 4,
         title: "Soluta est iure ex sequi a aspernatur.",
         content: "Eos et voluptatem sint tempore. Rerum voluptates quis est fugit voluptas sit voluptatem consequuntur. Voluptas sapiente illo quo optio.",
         created_at: "2019-10-12 21:05:10",
         updated_at: "2019-10-12 21:05:10",
       },
     ],
   }
>>>

GitHub - fzaninotto/Faker: Faker is a PHP library that generates fake data for you

Vue開発の際は

連載10回目でVue.jsを入れました。
デバッグはChromeの開発者ツールだけだといろいろしんどいので、Vue.jsのdevtoolsを入れるのをオススメします。

Vue.js devtools

入れるとこんな感じで、Chromeの開発者ツールに「Vue」タブが追加され、開発が捗ります。

vue.png
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?