Thursday, May 14, 2020

Laravel faker

/** @var \Illuminate\Database\Eloquent\Factory $factory */ $factory->define(App\Post::class, function (Faker\Generator $faker) { return [ 'title' => $faker->sentence, 'author_id' => function () { return factory(App\Author::class)->create()->id; }, 'body' => $faker->paragraphs(rand(3,10), true), ]; }); /** @var \Illuminate\Database\Eloquent\Factory $factory */ $factory->define(App\Author::class, function (Faker\Generator $faker) { return [ 'name' => $faker->name, 'bio' => $faker->paragraph, ]; }); $factory->define(App\Profile::class, function (Faker\Generator $faker) { return [ 'birthday' => $faker->dateTimeBetween('-100 years', '-18 years'), 'author_id' => function () { return factory(App\Author::class)->create()->id; }, 'city' => $faker->city, 'state' => $faker->state, 'website' => $faker->domainName, ]; });



public function run() { $authors = factory(App\Author::class, 5)->create(); $authors->each(function ($author) { $author ->profile() ->save(factory(App\Profile::class)->make()); $author ->posts() ->saveMany( factory(App\Post::class, rand(20,30))->make() ); }); }

// logging in appserviceprovider

DB::listen(function($query) { Log::info( $query->sql, $query->bindings, $query->time ); });

https://laravel-news.com/eloquent-eager-loading