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! :)
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.
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.
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 .
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With