5

我想知道如何将数据Input::all()与模型合并并保存结果。

澄清一下:我想做如下的事情:

$product = Product::find(1); // Eloquent Model

$product->merge( Input::all() ); // This is what I am looking for :)

$product->save();
4

3 回答 3

4

你应该使用update方法:

$product->update(Input::all());

但我建议改用only方法

$product->update(Input::only('name', 'type...'));
于 2014-06-12T09:44:31.917 回答
3

使用模型的fill()方法进行更好的控制。这使我们可以在保存之前合并值后更改属性:

$product->fill($request->all());
$product->foo = 'bar';
$product->save();

如果我们正确定义了模型的$fillable属性,则无需使用Input::only(...)(或$request->only(...)在较新的版本中)。

于 2017-10-19T00:30:05.757 回答
0

除了 Razor 的回答,如果您需要创建一个新模型,您可以使用:

$product = Product::create(Input::all());
于 2017-10-19T00:04:16.720 回答