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

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

我在尝试插入 table 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

这是我的控制器代码:

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 添加到 table,但 none 有效。 user_id 被插入到 users_basics table 但错误继续显示。

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

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();

它将在 table 中生成 Deleted_at 字段。

享受编码吧~!

您应该修改您的模型:

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

    protected $fillable = ['user_id'];
}