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

Viewed 19644

I am getting the following error while trying to insert into the 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

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:

 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_AT and PrimaryKey to the table but none worked. The user_id is being inserted into the users_basics table but the error continues to show.

2 Answers

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

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

it will make Deleted_at field in the table.

enjoy coding~!

Related