Laravel Model::create 或 Model->save()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27175551/
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
Laravel Model::create or Model->save()
提问by user13746
I'm wondering what approach should I use for storing data in the database;
我想知道我应该使用什么方法在数据库中存储数据;
First approach
第一种方法
$product = Product::create($request->all());
In my Product model I have my $filable array for mass assigment
在我的产品模型中,我有用于批量分配的 $filable 数组
Second approach
第二种方法
$product = new Product();
$product->title = $request->title;
$product->category = $request->category;
$product->save();
Is any of these two "Better solution"? What should I generally use?
这两个“更好的解决方案”中的任何一个?我一般应该用什么?
Thank you for you advice
谢谢你的建议
回答by Wader
Personal preference.
个人喜好。
Model::create()
relies on mass assignment which allows you to quickly dump data into a model, it works nicely hand-in-hand with validation rather than having to set each of the model properties manually. I've more recently started using this method over the latter and can say its a lot nicer and quicker.
Model::create()
依赖于质量分配,它允许您快速将数据转储到模型中,它可以很好地与验证协同工作,而不必手动设置每个模型属性。我最近开始使用这种方法而不是后者,可以说它更好更快。
Don't forget you also have a range of other mass assignment functions create()
, update()
and fill()
(Possibly more).
不要忘记您还有一系列其他质量分配功能create()
,update()
以及fill()
(可能更多)。