Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

faker

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! :)

like image 475
Simon Suh Avatar asked May 22 '17 18:05

Simon Suh


People also ask

What is make () In Laravel?

The make method will return an instance of the class or interface you request. Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class. E.g. $app->make('App\Services\MyService'); // new \App\Services\MyService.

What is difference between create and insert in Laravel?

create() is a function from Eloquent, insert() - Query Builder. In other words, create is just an Eloquent model function that handles the creation of the object to the DB (in a more abstract way). Insert however tries to create the actual query string.

What is difference between create and save in Laravel?

Save can be used to both create a new Record and update a existing record . Whereas create is used to create a new record by providing all required field at one time .

What are fakers in Laravel?

Faker is a PHP package that generates dummy data for testing. With Faker you can generate mass amount of testing data as you needed. Faker comes preinstalled in Laravel framework. You can also use Faker in other frameworks or your own native PHP websites.


2 Answers

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

like image 109
Alex Harris Avatar answered Oct 12 '22 00:10

Alex Harris


Laravel create method is created the model instance and save data in the database.
Make function has created the instance of the class.

click here for more info

like image 4
Chirag Prajapati Avatar answered Oct 11 '22 23:10

Chirag Prajapati