Laravel 5.2模型$ fillable被忽略了吗? [英] Laravel 5.2 Model $fillable gets ignored?

查看:188
本文介绍了Laravel 5.2模型$ fillable被忽略了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型IsolatedQuery,它由一个namequery字段组成.我已经在模型的$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仅要求namequery都可以填充任何值.

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的createupdate方法.
请阅读文档.

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的constructfill方法.之后,您可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆