Laravel 5.2模型$ fillable被忽略了吗? [英] Laravel 5.2 Model $fillable gets ignored?
问题描述
我有一个简单的模型IsolatedQuery
,它由一个name
和query
字段组成.我已经在模型的$fillable
属性中定义了这两个字段. IsolatedQueryController @ store看起来像这样:
I have a simple Model IsolatedQuery
which consists of a name
and query
field. I have defined those two fields in the $fillable
property of the model. The IsolatedQueryController@store looks like this:
public function store(IsolatedQueryRequest $request)
{
IsolatedQuery::insert($request->all());
session()->flash('flash_message', 'Isolated Query succesvol opgeslagen');
return redirect('iq');
}
为完整性起见,这是模型的出处(与我所描述的一样少)
For completeness, here is the Model's source (it is as little as I described it)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class IsolatedQuery extends Model
{
protected $fillable = [
'name',
'query'
];
}
IsolatedQueryRequest
仅要求name
和query
都可以填充任何值.
The IsolatedQueryRequest
only requires both name
and query
to be filled with any value.
使用给定名称和查询值调用store
方法时,出现错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list'
.
When calling the store
method with a given name and query value I get the error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list'
.
很明显,_token
字段随请求一起发送,但由于它未在$fillable
数组中列出,因此为什么尝试将其与实际的SQL查询一起存储有点困惑.
It's obvious a _token
field gets send with the request but I'm a bit baffled why it's trying to store it with the actual SQL query as it's not listed in the $fillable
array.
为什么要分配质量?
推荐答案
您使用查询生成器的方法insert
.它不检查fillable
.
您应该使用Eloquent的create
或update
方法.
请阅读文档.
You use Query Builder's method insert
. It doesn't check the fillable
.
You should use the create
or update
method of Eloquent.
Please, read the documentation.
此外,您可以将输入数据传递给Eloquent的construct
或fill
方法.之后,您可以使用save
方法.
Also, you may pass input data to construct
or fill
method of Eloquent. After that you may use the save
method.
这篇关于Laravel 5.2模型$ fillable被忽略了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!