php - Laravel 5.2 模型 $fillable 被忽略了吗?

标签 php laravel mass-assignment

我有一个简单的模型 IsolatedQuery,它由一个 namequery 字段组成。我已经在模型的 $fillable 属性中定义了这两个字段。 IsolatedQueryController@store 看起来像这样:

public function store(IsolatedQueryRequest $request)
{
    IsolatedQuery::insert($request->all());

    session()->flash('flash_message', 'Isolated Query succesvol opgeslagen');

    return redirect('iq');
}

为了完整起见,这是模型的来源(它和我描述的一样少)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class IsolatedQuery extends Model
{
    protected $fillable = [
        'name',
        'query'
    ];
}

IsolatedQueryRequest 只需要namequery 都填入任意值。

使用给定的名称和查询值调用 store 方法时出现错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list'

很明显 _token 字段随请求一起发送,但我有点困惑为什么它试图将它与实际的 SQL 查询一起存储,因为它没有在 $fillable< 中列出 数组。

为什么要批量分配?

最佳答案

您使用查询生成器的方法 insert。它不检查 fillable
你应该使用Eloquent的createupdate方法。
请阅读documentation .

此外,您可以将输入数据传递给 Eloquent 的 constructfill 方法。之后,您可以使用 save 方法。

关于php - Laravel 5.2 模型 $fillable 被忽略了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623631/

相关文章:

php - 如何在 Laravel 的数字字符串列上运行 where 子句?

php - 如何将文本输出到文件?

php - 约定表名(带下划线)

c++ - 位域赋值——安全吗?

ruby-on-rails-3 - 使用 RSpec 测试范围质量分配

php - 如何从批处理文件执行 codeigniter Controller

php - 为什么复合主键中的列顺序对 Doctrine 很重要?

javascript - Laravel 4 中的 JQuery 没有返回数据..为什么?

Laravel 5 模板存放目录

ruby-on-rails - 使用批量分配在 rails 4 中添加带有复选框的嵌套属性