Laravel Faker - What's the difference between create and make

Simon Suh picture Simon Suh · May 22, 2017 · Viewed 10.1k times · Source

I have the following code:

$this->actingAs(factory('App\User')->create());

$thread = factory('App\Thread')->make();

what is the difference between create() and make() and why is it not listed in the helper functions page in the Laravel documentation? Thank you! :)

Answer

Alex Harris picture Alex Harris · May 22, 2017

create persists to the database while make just creates a new instance of the model.

The create method not only creates the model instances but also saves them to the database using Eloquent's save method

https://laravel.com/docs/5.4/database-testing#using-factories

If you'd like to see the source code differences between make and create you can see them in src/Illuminate/Database/Eloquent/FactoryBuilder.php