【发布时间】:2014-07-03 05:11:16
【问题描述】:
我需要帮助,我在查询模型关系时遇到问题。 我实际上知道并使用查询方法完成了工作,但我想知道查询关系的“laravel 方式”。
这是我的控制器上的内容。 //健康档案控制器 //$id 值为 2
$health_profiles = User::find($id)->with('health_profiles')->first();
问题是查询的返回是id = 1而不是id = 2的记录。它基本上忽略了“find”方法。我只想获取特定 user_id 的健康档案。
[id] => 1
[firstname] => patrick
[lastname] => marino
[email] => patrick@gmail.com
[membership_code] => starpatrick
[birthdate] => 1989-05-17
[contact_number] => 1230123
[active] => 1
[created_at] => 2014-07-01 16:10:05
[updated_at] => 2014-07-01 16:10:05
[remember_token] =>
[health_profiles] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[parent_id] =>
[name] => patrick star
[relationship] =>
[gender] => male
[birthdate] => 1989-05-17
[marital_status] =>
[number_of_children] =>
[weigth] =>
[height] =>
[blood_type] =>
[blood_pressure] =>
[hdl] =>
[ldl] =>
[vldl] =>
[visual_activity] =>
[lifestyle] =>
[current_bmi] =>
[weight_goal] =>
[weekly_goal] =>
[rdc] =>
[created_at] => 2014-07-01 16:10:05
[updated_at] => 2014-07-01 16:10:05
)
这是我的架构 //用户模型
public function health_profiles()
{
return $this->hasMany('HealthProfile');
}
//健康档案模型
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
【问题讨论】: