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

标签 php laravel laravel-5.6

我在尝试插入表时遇到以下错误 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

这是我的 Controller 代码:

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 表中,但错误继续显示。

最佳答案

你应该修改你的模型:

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

    protected $fillable = ['user_id'];
}

关于php - Laravel 错误:函数 Illuminate\Database\Eloquent\Model::setAttribute() 的参数太少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53942124/

相关文章:

php - ExactOnline API 和 OData

php - 如何配置 PHP 以在运行时忽略 error_reporting()?

rest - 创建 REST API 时将动态资源 Controller 与 Laravel 嵌套的正确方法

node.js - 全新安装的 laravel 5.6 中 npm 安装错误

来自 iOS Swift 的 PHP AES256 PKCS7 解密

php - FPDF 中的表多单元格

php - 如何检查分页链接是否会显示在 Laravel Blade 中?

html - 如何保存和显示从texterea编辑器(tinymce)提交的数据?

laravel - 具有单个 Redis 实例的多个 Laravel 应用程序

php - Laravel 5.6 将 Eloquent 模型作为参数传递给函数