雄辩的模型关系返回为空
Posted
技术标签:
【中文标题】雄辩的模型关系返回为空【英文标题】:Eloquent model relationship returning as null 【发布时间】:2013-04-10 15:28:34 【问题描述】:我有一个 laravel 模型
class Project extends Eloquent
public static $timestamps = true;
public $includes = array('members','members.memberdata');
public function tasks()
return $this->has_many('Usertask','project_id');
public function members()
return $this->has_many('Projectmember','project_id');
及相关模型
class Projectmember extends Eloquent
public static $table = "project_members";
public static $timestamps = true;
public function project()
return $this->belongs_to('Project');
public function memberdata()
return $this->has_one('Usermetadata','user_id');
class Usermetadata extends Eloquent
public static $table = "users_metadata";
public function user()
return $this->belongs_to('User');
public function member()
return $this->belongs_to('Projectmember','user_id');
当我尝试像这样检索单个项目模型时
$project = Project::find($id);
return Response::eloquent($project);
我的 json 输出如下所示
"id":1,"user_id":1,"name":"UberWork","description":"Web based project management \/ task management app","target_date":"2013-11-15 00:00:00","budget":null,"status":0,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","members":["id":1,"project_id":1,"user_id":1,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":"user_id":1,"first_name":"Tamarakuro","last_name":"Foh","photo":"","company_name":null,"phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"12b14a7d3ca48c53bb5b1a88fa3eca3b","id":3,"project_id":1,"user_id":3,"created_at":"2013-04-16 20:13:59","updated_at":"2013-04-16 20:13:59","memberdata":"user_id":3,"first_name":"Ebere","last_name":"Uche","photo":"","company_name":"Chronotech Labs","phone":null,"package":"free","subscription_start":"0000-00-00 00:00:00","subscription_end":"0000-00-00 00:00:00","api_key":"ab446bd9ffbb898e818a892c7401e0f6","id":4,"project_id":1,"user_id":2,"created_at":"2013-04-17 08:13:00","updated_at":"2013-04-17 08:13:00","memberdata":null]
我的数据库是这样的;
用户
身份证 电子邮件 密码 ip_address 有效 ...users_metadata
身份证 user_id 名字 姓氏 个人资料照片 ...项目
身份证 user_id 姓名 说明 状态 ...项目成员
身份证 project_id user_id我的问题是为什么项目的最后一个成员的 memberdata 为“null”,而其他成员不为 null。我是不是做错了什么?
【问题讨论】:
您确定您的最后一个成员在 memberdata 表中有匹配的记录吗?您最后一个成员的id
和user_id
不匹配,我不知道这是否重要,但关系将匹配id
到另一个表的user_id
?
是的,users_metadata 表上有匹配记录
您能详细解释一下您的数据库设计吗? user_id
到处指的是什么?
我已经编辑了我的问题以包含数据库设计
问题是因为你想要project_members.user_id = users_metadata.user_id
,但是关系总是使用一个表的id
和另一个表的fk(user_id
)。这适用于您的大部分数据,因为 project_members.id
和 project_members.user_id
是相同的;当他们不是(您的最后一个项目成员)时,它会中断。
【参考方案1】:
Eloquent 中的关系总是将主键 (pk) 与外键 (fk) 联系起来。但是,您正在尝试基于两个外键建立关系,从而跳过了关系。唯一的 Eloquent 解决方案是包含额外的关系步骤。这里有一些模型(我已经省略了我们在这个例子中不需要的关系)......
class Project extends Eloquent
public static $timestamps = true;
public $includes = array('members','members.user', 'members.user.metadata');
public function members()
return $this->has_many('Projectmember','project_id');
class Projectmember extends Eloquent
public static $table = "project_members";
public static $timestamps = true;
public function user()
return $this->belongs_to('User');
class User extends Eloquent
public static $timestamps = true;
public $hidden = array('password', 'ip_address');
public function metadata()
return $this->has_one('Usermetadata');
class Usermetadata extends Eloquent
public static $table = "users_metadata";
public function user()
return $this->belongs_to('User');
我明白你为什么要跳过这段关系,但遗憾的是,这不可能在关系中实现。
【讨论】:
以上是关于雄辩的模型关系返回为空的主要内容,如果未能解决你的问题,请参考以下文章