Laravel 4 Illuminate\Database\Eloquent\MassAssignmentException 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22747061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:16:59  来源:igfitidea点击:

Laravel 4 Illuminate \ Database \ Eloquent \ MassAssignmentException error

phplaravellaravel-4eloquent

提问by user3478596

Hei, I already searched many answers out there but could not solve this problem.

嘿,我已经在那里搜索了很多答案,但无法解决这个问题。

Here is the code for my migration

这是我的迁移代码

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateActiveTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('activations', function($table)
        {
            $table->bigInteger('id')->primary();
            $table->tinyInteger('token');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('activations');
    }
}

For model (models/Activation.php)

对于模型(models/Activation.php

<?php

class Activation extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'activations';
    protected $guarded = array();
}

And i am calling Activation table like this.

我像这样调用激活表。

Activation::create(['id' => 2, 'token' => 1231]);

Seriously i have no idea what's wrong here. And i am newbie at laravel 4. Hope somebody with teach me whats happening and how to solve it.

说真的,我不知道这里出了什么问题。我是 laravel 4 的新手。希望有人教我发生了什么以及如何解决它。

回答by Marwelln

You need to use the $fillableproperty in your Activationclass when you use Mass Assignment.

使用Mass Assignment时,您需要$fillableActivation类中使用该属性。

class Activation extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'activations';

    protected $fillable = ['id', 'token'];
}