Model factories are a great feature of Laravel that will help us get information into our database. Normally we would create a list of users and then insert them into our database using a seeder.

Here's an example of inserting data into our database without model factories:

$users = [
    ['name' => 'Chris', 'email' => 'chris@scotch.io'],
    ['name' => 'Nick', 'email' => 'nick@scotch.io'],
    ['name' => 'Holly', 'email' => 'holly@scotch.io'],
];

foreach ($users as $user)
    App\User::create($user);

Here's the example with model factories:

factory(App\User::class, 3)->create();

Much easier!

Chris Sevilleja

162 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.