【发布时间】:2017-10-22 11:55:53
【问题描述】:
我有以下代码:
$this->actingAs(factory('App\User')->create());
$thread = factory('App\Thread')->make();
create() 和 make() 有什么区别,为什么 Laravel 文档的帮助函数页面中没有列出?谢谢! :)
【问题讨论】:
我有以下代码:
$this->actingAs(factory('App\User')->create());
$thread = factory('App\Thread')->make();
create() 和 make() 有什么区别,为什么 Laravel 文档的帮助函数页面中没有列出?谢谢! :)
【问题讨论】:
create 持久化到数据库中,而make 只是创建模型的一个新实例。
create方法不仅可以创建模型实例,还可以保存 使用 Eloquent 的保存方法将它们保存到数据库中
https://laravel.com/docs/5.4/database-testing#using-factories
如果您想查看 make 和 create 之间的源代码差异,可以在 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.
【讨论】: