从 laravel/lumen 中的两个 eloquent 关系模型获取数据的正确方法是什么?

What is the proper way to get data from two eloquent relational model in laravel/lumen?

我正在尝试与一些 eloquent table 建立关系,并尝试使 api 实际上从两个关系 table 检索数据。放一张图就明白了。

让我们考虑员工和 employee_types 以简化问题。我的迁移代码看起来像

        Schema::create('os_employee_types', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->integer('priority')->default(0);
        $table->tinyInteger('status')->default('1');
        $table->timestamps();
    });

并且,

    Schema::create('os_employees', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->bigInteger('employee_type')->unsigned()->nullable();
        $table->timestamps();
        $table->foreign('employee_type')->references('id')->on('os_employee_types');
    });

在员工table中,employee_type是员工类型table中的外键,与员工类型table的id相关。我的模型看起来像

class OsEmployeeType extends Model
 {
public function OsEmployee()
{
    return $this->hasOne(OsEmployee::class);
}
 }

并且,

 class OsEmployee extends Model
  { 
public function OsEmployeeType()
{
    return $this->belongsTo(OsEmployeeType::class);
}    
 }

现在控制器看起来像

   public function check(){
$employee = OsEmployee::with('OsEmployeeType')->get();
return new OsEmployeeMaxCollection($employee);
}

我收到回复了

{
"data": [
    {
        "id": 56,
        "name": "Arafat Rahman",
        "employee_type": 2,
        "created_at": "2022-03-01T11:23:05.000000Z",
        "updated_at": "2022-03-01T11:23:05.000000Z",
    }
]
 }

这是来自 os_employee table 的绝对数据。但我想要员工类型的数据。我想要

这样的回复
{
"data": [
    {
        "id": 56,
        "name": "Arafat Rahman",
        "employee_type": {
               "id": 2,
                "name": "employee_type 1",
                 "priority": 10,
                  "status": 1
          },
        "created_at": "2022-03-01T11:23:05.000000Z",
        "updated_at": "2022-03-01T11:23:05.000000Z",
       
    }
]
 }

这是 dd($employee)

OsEmployeeMaxCollection 看起来像

class OsEmployeeMaxCollection extends ResourceCollection
   {
/**
 * Transform the resource collection into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
    return parent::toArray($request);
}
  }

我的错误是什么?我怎样才能得到像我描述的数据。

public function check(){
    
    return OsEmployee::with('OsEmployeeType')->get();
}

这应该有效。如果没有 OsEmployeeType,则此字段不应为空。

如果你想要json回复

public function check(){
    
    return OsEmployee::with('OsEmployeeType')->get()->toJson();
}

并将您的代码迁移更改为:

   Schema::create('os_employees', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->bigInteger('employee_type_id')->unsigned()->nullable();
        $table->timestamps();
        $table->foreign('employee_type_id')->references('id')->on('os_employee_types');
    });

你的模特:

 class OsEmployee extends Model
  { 
public function OsEmployeeType()
{
    return $this->belongsTo(OsEmployeeType::class, 'employee_type_id');
}    
 }