Published in Laravel

Laravel Application Helper Function: factory

By John Koster

factory()

The factory function is used to create Eloquent models, generally used when testing an application. The factory method can generally take one of four forms:

1<?php
2
3// Return an instance of the 'User' model.
4$user = factory('App\User')->make();
5
6// Return two instances of the 'User' model.
7$users = factory('App\User', 2)->make();
8
9// Return one instance of the 'User' model, with some modifications
10// that were defined earlier and bound to the 'admin' description.
11$adminUser = factory('App\User', 'admin')->make();
12
13// Return two instances of the 'User' model, with some modifications
14// that were defined earlier and bound to the 'admin' description.
15$adminUsers = factory('App\User', 'admin', 2)->make();