Using factories for migrations and tests

Factories are a newer feature in Laravel 5.1. It makes it very easy to populate models with Faker data: https://github.com/fzaninotto/Faker. I will cover using factories in tests. In the How it works… section of the same recipe, I will explain more about the process, including why we use it.

Getting ready

A fresh install of Laravel, Homestead setup for migrations is needed. Then follow the prior recipe for altering a migration:

Getting ready

How to do it…

Follow these steps to use factories:

  1. First, we will show this in a test so let's type:
    >php artisan make:test ExampleFactoryTest
    
  2. Then, open that file and we will create a ...

Get Laravel 5.x Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.