简体   繁体   English

我如何能够使用带有 eloquent 和 laravel 的嵌套预加载获取另一个表中的外键名称

[英]How would I be able to get the foreign key name in another table using nested eager loading with eloquent and laravel

So I currently have 3 models Specie Type User .所以我目前有 3 个模型Specie Type User I want to be able to get the name of the last modified user in relation to the Specie model我希望能够获得与 Specie 模型相关的最后修改用户的名称

The relationships are as follows关系如下

Class Specie extends Model {
   public function type()
   {
        return $this->belongsTo('App\Type', 'type_id');
   }

   public function user()
   {
        return $this->belongsTo('App\User', 'user_id');
   }
}

Class Type extends Model {
   public function specie()
   {
        return $this->hasMany('App\Specie', 'type_id');
   }
}

Class User extends Model {
   public function specie()
   {
        return $this->hasMany('App\Specie', 'user_id');
   }
}

I have tried this我试过这个

Class Specie extends Model {
    public function lastModified()
    {
        return $this->belongsTo('App\User', 'last_modified_by');
    }
}

And then used the below code然后使用下面的代码

$this->type->with(['specie.lastModified' => function ($query) use 
        ($userId) {
            $query->where('user_id', $userId);
        }])->get();

Unfortunately this does not seem to work不幸的是,这似乎不起作用

However, by using this code但是,通过使用此代码

$this->type->with(['specie' => function ($query) use ($userId) {
            $query->where('user_id', $userId);
        }])->get();

I am able to get this:我能够得到这个:

"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
  {
    "id": 6,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "last_modified_by": 1,
  },
  {
    "id": 7,
    "user_id": 1,
    "type_id": 2,
    "note": "sa",
    "last_modified_by": 2,
  },
]

However, what I want to get is the name of the last modified person name which is the primary key in the User model and the foreign key in the Specie model但是,我想得到的是最后修改的人名的名称,它是 User 模型中的主键和 Specie 模型中的外键

This is what I expect to get:这是我期望得到的:

"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
  {
    "id": 6,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "last_modified_by": 1,
    "user": [
        {
         "id": 1,
         "user": 'gerrard'
        }
    ]
  },
  {
    "id": 7,
    "user_id": 1,
    "type_id": 2,
    "note": "sa",
    "last_modified_by": 2,
    "user": [
         {
           "id": 2,
           "user": 'chris'
         }
    ]
  }
]

A with() inside the with() should do it: with()with()应该这样做:

$this->type->with(['specie' => function ($query) use ($userId) {
    $query->where('user_id', $userId);
    $query->with('lastModified');
}])->get();

The only difference is the relation key is not named "user" but rather name of the relationship ( "last_modified" ).唯一的区别是关系键没有命名为"user" ,而是关系的名称( "last_modified" )。

"id": 1,
"type": "Halo",
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
    {
        "id": 6,
        "user_id": 1,
        "type_id": 1,
        "last_modified_by": 2,
        "note": "et",
        "last_modified": {
            "id": 2
        }
    }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM