Laravel 4 Illuminate \ Database \ Eloquent \ MassAssignmentException error

吃可爱长大的小学妹 提交于 2019-12-01 14:39:51

问题


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)

<?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.


回答1:


You need to use the $fillable property in your Activation class when you use Mass Assignment.

class Activation extends Eloquent {

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

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


来源:https://stackoverflow.com/questions/22747061/laravel-4-illuminate-database-eloquent-massassignmentexception-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!