[英]Laravel Model::create or Model->save()
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 谢谢你的建议
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. Model::create()
依赖于质量赋值,它允许您快速将数据转储到模型中,它与验证很好地协同工作,而不必手动设置每个模型属性。 I've more recently started using this method over the latter and can say its a lot nicer and quicker. 我最近开始使用这种方法而不是后者,可以说它更好更快。
Don't forget you also have a range of other mass assignment functions create()
, update()
and fill()
(Possibly more). 不要忘记你还有一系列其他质量赋值函数create()
, update()
和fill()
(可能更多)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.