Laravel错误:函数Illuminate \ Database \ Eloquent \ Model :: setAttribute()的参数太少

时间:2018-12-27 08:43:51

标签: php laravel laravel-5.6

尝试插入表users_basics

时出现以下错误
  

Illuminate \ Database \ Eloquent \ Model :: setAttribute(),传入1   C:\ xampp \ htdocs \ msadi \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Eloquent \ Concerns \ HasAttributes.php   在第592行,预期恰好2个

这是我的控制器代码:

public function create()
{
   $userId = '10';

   $userBasics = new UserBasics;
   $userBasics->user_id = $userId;
   $userBasics->save();

   return redirect('users');
}

这是我的模特:

class UserBasics extends Model
{
    protected $table = 'users_basics';
    protected $primaryKey = null;

    protected $fillable = ['user_id'];

    const UPDATED_AT = null;
    const CREATED_AT = null;
}

这是我的user_basics迁移:

 public function up()
    {
        Schema::create('users_basics', function (Blueprint $table) {
            $table->integer('user_id');
            $table->bigInteger('adhaar_no')->nullable();
            $table->string('mobile_no')->nullable();
            $table->string('college_roll_no')->nullable();
            $table->date('dob')->nullable();

            $table->index('user_id');
        });
    }

我尝试将UPDATED_ATCREATED_ATPrimaryKey添加到表中,但是没有一个起作用。 user_id被插入到users_basics表中,但错误继续显示。

2 个答案:

答案 0 :(得分:2)

您应该修改模型:

class UserBasics extends Model 
{
    protected $table = 'users_basics';
    protected $primaryKey = null;
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = ['user_id'];
}

答案 1 :(得分:0)

因为您没有“时间戳”字段。 请这样添加。

public function up()
    {
        Schema::create('sadi_users_basics', function (Blueprint $table) {
            $table->integer('user_id');
            $table->bigInteger('adhaar_no')->nullable();
            $table->string('mobile_no')->nullable();
            $table->string('college_roll_no')->nullable();
            $table->date('dob')->nullable();
            $table->timestamp(); <---- just insert this.
            $table->index('user_id');
        });
    }

此外,如果要使用软删除,请添加$table->softDeletes();

它将在表中显示Deleted_at字段。

享受编码〜!