【问题标题】:How to get foreign key name of loaded relationship using laravel eloquent如何使用 laravel eloquent 获取加载关系的外键名称
【发布时间】:2020-01-04 04:41:12
【问题描述】:

所以我目前有3个模型SpecieTypeUser。我希望能够获得与Specie 模型相关的最后修改用户的名称

关系如下

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

我试过了

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

然后使用下面的代码

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

不幸的是,这似乎不起作用

但是,通过使用此代码

$this->type->with(['specie' => function ($query) use ($userId) {
            $query->where('user_id', $userId);
        }])->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,
  }
]

但是,我想得到的是最后修改人名的名字,它是User 模型中的主键和Specie 模型中的外键

这是我期望得到的:

"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'
  }
]

【问题讨论】:

  • 你没有lastModified的型号??
  • LastModified 不是模型
  • 所以首先创建这个模型并在模型中定义你的表名..ok
  • 没有表lastModified... Specie表有2个外键user_id和last_modified_by last_modified_by的值是来自用户模型的用户id...用户模型有一个名字,所以我想得到那个用户的名字。请再读一遍问题

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:

来自您的 Eloquent 实体关系:

Specie 和 Type(type) 相关(一对多)

Specie 和 User(user) 相关(一对多)

Specie 和 User(lastModified) 相关(一对多)

User 和 Type 是不相关的

因此,要包含用户名,您必须通过lastModified() 方法查询Specie 模型。

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

你应该期待这样的结果

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

从结果来看,specie 中的字段(外键)last_modified_by 值 1 应该与 user 中的字段 id 匹配。

另外,我假设您的用户名字段是name。它将包含在返回的用户数据中。

更新

您可以不使用即时加载通过Type 表从User 表中获取任何用户数据,除非它们是相关的。因此,模型必须相关(即相互约束)才能嵌套,例如使用急切加载。请参阅此medium article 以更好地理解这一点。

【讨论】:

  • 您是否在Specie 模型中包含lastModified() 方法?另外,您从哪里使用查询$this->...?
  • 这不是我所期望的,你能检查一下我的问题中的预期结果吗?
  • 如果您没有得到预期的结果,那么"last_modified_name" 不是您的Specie 表中的字段。是吗?
  • 是User表中的一个字段
  • 我需要获取类型名称作为主要对象而不是用户。我希望用户成为物种数组的一部分
【解决方案2】:

我在相关模型上使用了getKeyName()方法。

我的关系是一对多的,所以返回为Collection。 访问集合 [0] 中返回的集合的第一项,然后运行 ​​getKeyName() 有效。

在我的代码下面首先检查关系数据是否已填充,是否作为集合(多个)返回,并且至少有 1 个项目。

if(
$item->relatives 
&& $item->relatives instanceof \Illuminate\Database\Eloquent\Collection 
&& count($item->relatives)
) {

     $relation_key = $item->relatives[0]->getKeyName();
}

我认为您可以测试关系是作为模型集合返回还是返回单个模型。

如果返回单个模型,您可以: $item->relative->getKeyName();

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2020-10-20
    • 1970-01-01
    • 2019-08-15
    • 2020-02-28
    • 1970-01-01
    相关资源
    最近更新 更多