Laravel Eloquent 关系属于 returns 当前 table 而不是加入 table

Laravel Eloquent relationship belongsTo returns current table instead of the joined table

我正在尝试通过这段代码获取这些数据 Auth::user()->personnel->currentAssignment->role->is_allowed 但我得到了空值,因为假设角色值 return 赋值,而不是角色数据。

到目前为止,这工作正常 Auth::user()->personnel->currentAssignment,但是一旦我加入 role,我得到的是 PersonnelAssignment 数据而不是 RoleCatelog,这导致 null价值

请问我是 Laravel 的新手。我需要你的帮助。

如你所见return这个数据:

"current_assignment": {
            "id": "778",
            "personnel_id": "100751",
            "area_id": 154,
            "role_id": 17,
            "dept_id": 154,
            "start_date": "2017-11-08",
            "end_date": null,
            "is_temporary": null,
            "is_deleted": null,
            "modify_id": "Sample Name",
            "modify_dt": null,
            "create_id": "Administrator",
            "create_dt": null,
            "transfer_id": null,
            "transfer_dt": null,
            "role": {
                "id": "101",
                "personnel_id": null,
                "area_id": 154,
                "role_id": 17,
                "dept_id": 154,
                "start_date": "2007-05-04",
                "end_date": null,
                "is_temporary": null,
                "is_deleted": null,
                "modify_id": "Sample Name II",
                "modify_dt": null,
                "create_id": "Sample Name II",
                "create_dt": null,
                "transfer_id": null,
                "transfer_dt": null
            }
        }

这是分配模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;

class PersonnelAssignment extends Model
{
    protected $table = 'smed_personnel_assignment';

    protected $primaryKey = 'id';
    protected $keyType = 'string';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'id',
        'personnel_id',
        'area_id',
        'role_id',
        'dept_id',
        'start_date',
        'end_date',
        'modify_id',
        'modify_dt',
        'create_id',
        'create_dt',
    ];


    public function role(){
        return $this->belongsTo(RoleCatalog::class, 'role_id');
    }

}

这是榜样...

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RoleCatalog extends Model
{
    protected $table = 'smed_personnel_assignment';

    protected $primaryKey = 'role_id';
    public $incrementing = false;
    public $timestamps = false;

    protected $fillable = [
        'role_id',
        'role_name',
        'role_desc',
        'role_area',
        'is_allowed',
        'is_deleted',
        'modify_id',
        'modify_dt',
        'create_id',
        'create_dt',
    ];


}

..

看到 RoleCatalog 模型的主键是 role_id 我会尝试改变关系如下:

class PersonnelAssignment extends Model
{    
    public function role(){
        return $this->belongsTo(RoleCatalog::class, 'role_id', 'role_id');
    }
}

class RoleCatalog extends Model
{
    public function personnelAssignment(){
        return $this->hasMany(PersonnelAssignment::class, 'role_id', 'role_id');
    }
}

顺便说一句,我会更改 RoleCatalog 模型的 table 名称,现在它设置为与 PersonnelAssignment 相同的 table;两种型号都有 protected $table = 'smed_personnel_assignment';

.