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

发布于 2025-01-11 08:45:17 字数 2718 浏览 0 评论 0原文

我正在尝试与一些雄辩的表建立关系,并尝试制作实际上从两个关系表中检索数据的 api。这里一张照片就明白了。

输入图片这里的描述

让我们考虑员工和员工类型来简化问题。我的迁移代码看起来像这样

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

在员工表中,employee_type 是员工类型表中的外键,与员工类型表上的 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 表的绝对数据。但我想要具有员工类型的数据。我想要这样的响应:

{
"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",
       
    }
]
 }

Here is the 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);
}
  }

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

I am trying to build up a relation with some eloquent table and try to make api that actually retrieve data from two relational table. Here a photo to understand.

enter image description here

Lets consider employees and employee_types for simplify the problem. My migration code looks like

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

And,

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

Here in employee table, employee_type is a foreign key in employee types table that relation with id on employee type table. And my model looks like

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

And,

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

Now the controller looks like

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

I got the response

{
"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",
    }
]
 }

This is absolute data from os_employee table. but I want the data with employee type. I want the response like

{
"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",
       
    }
]
 }

Here is the dd($employee)

enter image description here

And OsEmployeeMaxCollection looks like

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

What was my mistake or wrong? How can I get data like my description.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2025-01-18 08:45:17
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');
}    
 }
public function check(){
    
    return OsEmployee::with('OsEmployeeType')->get();
}

This should work. If it does not have OsEmployeeType this field should not be empty.

If you want json reponse

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

and change your code migration to:

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

And your model to:

 class OsEmployee extends Model
  { 
public function OsEmployeeType()
{
    return $this->belongsTo(OsEmployeeType::class, 'employee_type_id');
}    
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文