错误“调用未定义的方法Illuminate \ Auth \ GenericUser".访问Auth :: user()的hasMany关系时 [英] Error "Call to undefined method Illuminate\Auth\GenericUser" when accessing to hasMany relationship of Auth::user()

查看:169
本文介绍了错误“调用未定义的方法Illuminate \ Auth \ GenericUser".访问Auth :: user()的hasMany关系时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户和患者之间定义了一对多关系,但是当我尝试与经过身份验证的用户保存新的患者记录时,会出现错误

I defined a relationship One-to-Many between User and Patient, but when I try to save a new patient record with the authenticated user I get the error

调用未定义的方法Illuminate \ Auth \ GenericUser :: Patients()

Call to undefined method Illuminate\Auth\GenericUser::patients()

这是我的桌子:

// Users table
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->rememberToken();
    $table->timestamps();
    $table->softDeletes();
});

// Patients table
Schema::create('patients', function (Blueprint $table) {
    $table->increments('id');
    //Foreign key
    $table->integer('user_id')->unsigned();
    $table->string('ci')->unique();
    $table->string('name');
    $table->string('last_name');
    $table->string('gender');
    $table->date('birth_date')->nullable();
    $table->string('place')->nullable();
    $table->timestamps();
    $table->softDeletes();

    $table->foreign('user_id')
          ->references('id')->on('users');
});

在PatientController中,我调用Auth :: user()来保存新患者:

In PatientController I call Auth::user() to save a new patient:

public function store(PatientRequest $request){
    $patient = new Patient($request->all());
    Auth::user()->patients()->save($patient);
    $last = Patient::get()->last();

    return redirect()->route('patient.histories.create', [$last->id])->with('message', 'Success!');
}

关系定义如下:

// IN USER MODEL
public function patients(){
    return $this->hasMany('App\Patient');
}

// IN PATIENT MODEL
public function user(){
    return $this->belongsTo('App\User');
}

目前我真的不知道出什么问题了,但是当我通过修补匠创建新的患者记录时,它会按预期工作:

At this point I really don't know what is wrong, but when I create a new patient record from tinker it works as expected:

>>> $patient = new App\Patient;
>>> $patient->ci = "1234567";
.......
.......

>>> $user = App\User::first();
>>> $user->patients()->save($patient);

请问有人可以找出错误在哪里吗?

Can someone spot where is the error, please?

推荐答案

我发现了问题所在,我对auth.php文件内的providers数组中的两个选项都未加注释:

I found out what was the problem, I left uncommented both options in the providers array inside the auth.php file:

'providers' => [
        'users' => [
                'driver' => 'eloquent',
                'model' => App\User::class,
        ],

        // ERROR
        // Call to undefined method Illuminate\Auth\GenericUser::patients()

        // 'users' => [
                //'driver' => 'database',
                //'table' => 'users',
        // ],
],

这篇关于错误“调用未定义的方法Illuminate \ Auth \ GenericUser".访问Auth :: user()的hasMany关系时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆