How to quickly generate data using faker and tinker in laravel


Posted 4 months ago by Ryan Dhungel

Category: Laravel

Viewed 304 times

Estimated read time: 1 minute

You can generate fake data for any model and any fields you want in less than a minute using the existing tools that are available in laravel. They are factory and tinker.

This is a great way of working with laravel during development. This is how you can do it.

Step 1.


Go to database/factories/ModelFactories.php

 

$factory->define(App\Note::class, function (Faker\Generator $faker) {
static $password;

return [
'user_id' => 1,
'title' => $faker->sentence(10),
'body' => $faker->sentence(30),
];
});

Step 2.


In terminal run php artisan tinker then..

factory(App\Note::class, 10)->create();

You just created 10 instance of notes with title (10 sentences) and body (30 sentences). Happy Coding!