简体   繁体   English

如何使用laravel雄辩获取已加载关系的外键名称

[英]How to get foreign key name of loaded relationship using laravel eloquent

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": 5,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "created_by": 1,
    "last_modified_by": 1,
  }
]

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": 5,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "created_by": 1,
    "last_modified_by": 1,
    "last_modified_name": 'gerrard'
  }
]

in your model like this 在这样的模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class lastModified extends Model
{
    protected $table = 'lastModifieds';

}

From your Eloquent entity relationships: 从您雄辩的实体关系中:

Specie and Type(type) are related (one-to-many) SpecieType(type)是相关的(一到多)

Specie and User(user) are related (one-to-many) SpecieUser(user)相关(一对多)

Specie and User(lastModified) are related (one-to-many) SpecieUser(lastModified)相关(一对多)

User and Type are not related UserType 相关

So, for you to include the user name you have to query the Specie model through the lastModified() method. 因此,要包括用户名,您必须通过lastModified()方法查询Specie模型。

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

You should expect a result like this 您应该期望这样的结果

"id": 1,
"name": "the_user_name" 
// any other user data
"created_at": "2019-07-20 13:02:53",
"updated_at": "2019-07-20 13:02:53",
"specie": [
  {
    "id": 5,
    "user_id": 1,
    "type_id": 1,
    "note": "et",
    "created_by": 1,
    "last_modified_by": 1,
  }
]

From the result, the field(foreign key) last_modified_by in specie with value 1 should match the field id in user . 从结果来看, specie的字段(外键) last_modified_by值为1应该与user中的字段id匹配。

Also, I'm assuming the field for your user name is name . 另外,我假设您的用户名字段为name It would be included with the returned user data. 它会包含在返回的用户数据中。

UPDATE 更新

You can not get any user data from the User table through the Type table using eager loading except they are related. 不能使用急切的加载通过Type表从User表中获取任何用户数据,除非它们是相关的。 Ergo, models must be related (ie constrained by each other) before they can be nested, for instance using eager loading. 因此,必须先关联(即相互约束)模型,然后才能嵌套它们,例如使用急切的加载。 See this medium article to better understand this. 请参阅本文,以更好地理解这一点。

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

相关问题 我如何能够使用带有 eloquent 和 laravel 的嵌套预加载获取另一个表中的外键名称 - How would I be able to get the foreign key name in another table using nested eager loading with eloquent and laravel Laravel Eloquent 与不同外键的关系 - Laravel Eloquent Relationship with different foreign key Laravel - 使用 Eloquent 中的外键获取表之间的数据 - Laravel - Get data between tables using foreign key in Eloquent 如何使用Laravel获取属于外键的用户名? - How to get the name of user that belongs to foreign key using Laravel? 如何获取 Laravel Eloquent 关系中使用的外键方法? - How to get foreign key's method used in Laravel Eloquent Relationships? 如何从laravel的雄辩集合中获取外键 - How to get foreign key from eloquent collection in laravel 如何使用laravel雄辩地访问多个外键 - How to access multiple foreign key using laravel eloquent Laravel Eloquent Relationship: How to get parent of parent information from child using Laravel eloquent model relationship? - Laravel Eloquent Relationship: How to get parent of parent information from child using Laravel eloquent model relationship? Laravel Eloquent 关系——表的多列引用同一个外键 - Laravel Eloquent relationship - multiple columns of table reference same foreign key 使用Eloquent检索外键名称 - Retrieve name of foreign key using Eloquent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM