【发布时间】:2016-09-24 18:53:06
【问题描述】:
在我的 laravel 5.2 网络应用程序中,我有以下三个表-
用户
id
username
个人资料
id
user_id
profile_images
id
profile_id
概述我的场景 - 用户可以有一个个人资料(一对一),一个个人资料可以有许多个人资料图片(一对多)。这些具有相关关系函数的表的模型是-
用户.php
public function profile_images(){
$this->hasManyThrough('App\ProfileImage', 'App\Profile');
}
public function profile(){
return $this->hasOne('App\Profile');
}
Profile.php
public function profile_images()
{
return $this->hasMany('App\ProfileImage');
}
public function user()
{
return $this->belongsTo('App\User');
}
ProfileImage.php
public function profile()
{
return $this->belongsTo('App\Profile');
}
我的问题 当我在用户模型上调用 profile_images() 时,我只获取用户数据,并且只获取配置文件和 profile_images 的列名。即使我有一个配置文件(与我的测试用例匹配的 user_id)和两个具有匹配 profile_id 的 profile_image,也不会返回任何属性/数据。
你能帮忙吗?我假设这是我使用 hasManyThrough 的问题。
谢谢
【问题讨论】:
-
你能运行get方法吗?像这样的东西。 $user->profile_images()->get();
标签: php mysql orm laravel-5 eloquent