Laravel Faker - create 和 make 之间有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44119401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:01:34  来源:igfitidea点击:

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

phplaravelfaker

提问by Simon Suh

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

create() 和 make() 之间有什么区别,为什么它没有列在 Laravel 文档的辅助函数页面中?谢谢!:)

回答by Alex Harris

createpersists to the database while makejust creates a new instance of the model.

create持久化到数据库,同时make只创建模型的新实例。

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

create方法不仅创建模型实例,还使用 ​​Eloquent 的 save 方法将它们保存到数据库中

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

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

如果您想查看 make 和 create 之间的源代码差异,可以在 src/Illuminate/Database/Eloquent/FactoryBuilder.php

回答by Chirag Prajapati

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

点击这里获取更多信息