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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53942124/
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 18:13:59  来源:igfitidea点击:

Laravel Error: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute()

phplaravellaravel-5.6

提问by Tauseef Shah

I am getting the following error while trying to insert into the table users_basics

尝试插入表时出现以下错误 users_basics

Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in C:\xampp\htdocs\msadi\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php on line 592 and exactly 2 expected

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

Here is my controller code:

这是我的控制器代码:

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

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

   return redirect('users');
}

Here is my model:

这是我的模型:

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

    protected $fillable = ['user_id'];

    const UPDATED_AT = null;
    const CREATED_AT = null;
}

Here is my user_basics migration:

这是我的 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');
        });
    }

I have tried adding UPDATED_AT, CREATED_ATand PrimaryKeyto the table but none worked. The user_idis being inserted into the users_basicstable but the error continues to show.

我曾尝试将UPDATED_AT,CREATED_AT和添加PrimaryKey到表中,但没有任何效果。在user_id被插入到users_basics表中,但错误继续显现。

回答by Nguyen Hung Thai

You should modify your model:

你应该修改你的模型:

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

    protected $fillable = ['user_id'];
}

回答by JsWizard

because you not have field for Timestamp. please add like this.

因为你没有时间戳字段。请像这样添加。

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');
        });
    }

also, if you want to use softdelete then add $table->softDeletes();

另外,如果你想使用 softdelete 然后添加 $table->softDeletes();

it will make Deleted_at field in the table.

它将在表中创建 Deleted_at 字段。

enjoy coding~!

享受编码~!