是否可以使用Input::all()创建新记录



我使用的是Laravel 4.x。是否可以传递Input:all(),而不是设置对象的单个属性,然后调用save()

我的表单字段在命名约定上类似于数据库字段。

从laravel文档中,您可以选择-

$user = User::create(array('name' => 'John'));
// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));
// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(array('name' => 'John'));

您可以将所有这些用作以下

$user = User::create(Input::all());
// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(Input::all());
// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(Input::all());

但您需要注意表单字段名和数据库列名是相同的。

此外,你必须在你的模型上寻找$guarded。这些字段将无法以这种方式插入。

您可以使用Model::create(Input::all())

为此,您需要在模型中指定受保护的$fillable数组,该数组指定可大量分配的字段。

protected $fillable=array('column1','cloumn2');

最新更新