Laravel雄辩ORM关系查询

Laravel雄辩ORM关系查询,laravel,eloquent,Laravel,Eloquent,我需要帮助,我在查询模型关系时遇到问题。 我实际上知道并使用查询方法完成了工作,但我想知道查询关系的“laravel方式” 这是我的控制器上的内容。 //HealthProfile控制器 //$id值为2 $health_profiles = User::find($id)->with('health_profiles')->first(); 问题是查询返回的是id=1而不是id=2的记录。它基本上忽略了“查找”方法。我只想获取特定用户id的运行状况配置文件 [id] =>

我需要帮助,我在查询模型关系时遇到问题。 我实际上知道并使用查询方法完成了工作,但我想知道查询关系的“laravel方式”

这是我的控制器上的内容。 //HealthProfile控制器 //$id值为2

$health_profiles = User::find($id)->with('health_profiles')->first();
问题是查询返回的是id=1而不是id=2的记录。它基本上忽略了“查找”方法。我只想获取特定用户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');        
        }
//HealthProfile模型

public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id');
    }
试试这个

User::with('health_profiles')->find($id);
我认为您不需要调用
first
方法,因为前面提到的
find
只会找到您需要的一行数据。

试试这个

User::with('health_profiles')->find($id);

我认为您不需要调用
first
方法,因为前面提到的
find
只会找到您需要的一行数据。

您可以尝试将
with
放在
find
之前,以便构建器知道如何在您试图查找的内容上加载该关系

$user = User::with('health_profiles')->find($user_id);
$health_profiles = $user->health_profiles;

您可以尝试将
with
放在
find
之前,以便构建器知道如何在您试图查找的内容上加载该关系

$user = User::with('health_profiles')->find($user_id);
$health_profiles = $user->health_profiles;

我找到了罪犯。L4有蛇壳方法的问题!我把它改成了camelCase,它成功了

$lawly = User::where('id', '=', 2)->first();
$lawly->healthProfiles->toArray()

src:

我找到了罪犯。L4有蛇壳方法的问题!我把它改成了camelCase,它成功了

$lawly = User::where('id', '=', 2)->first();
$lawly->healthProfiles->toArray()

src:

首先,我要解释几句:

find($id)
已经运行了查询(它使用
where(id,$id)->first()
在引擎盖下),所以将其放在末尾,因为现在您无意中这样做了:

User::find($id);
User::with('health_profiles')->first();
另一个问题是,正如您已经注意到的,
elount
无法使用此设置:

public function health_profiles() ...

$user = User::find($id);
$user->health_profiles; // null
因为在加载动态属性(关系)时,它会在模型上查找camelCased方法

但是,快速加载将按预期工作:

$user = User::with('health_profiles')->find($id);
$user->health_profiles; // related model/collection
所以,如果你想成为你的朋友,你一定要遵守命名规则


但这还不是全部。它将以另一种方式工作:

public function healthProfiles() ...

$user = User::find($id);
$user->healthProfiles; // works, returns related model/collection
$user->health_profiles; // works as well, returns the model/collection

总结并回答您的问题,以下每一项都适用于您:

// Assuming Profile is the model

// 2 queries
$user = User::find($id);
$profiles = $user->healthProfiles;

// or 1 query
$profiles = Profile::where('user_id', $id)->get();

// or 2 queries: 1 + 1 subquery
$profiles = Profile::whereHas('user', function ($q) use ($id) {
   $q->where('users.id', $id);
})->get();

首先是几句解释:

find($id)
已经运行了查询(它使用
where(id,$id)->first()
在引擎盖下),所以将其放在末尾,因为现在您无意中这样做了:

User::find($id);
User::with('health_profiles')->first();
另一个问题是,正如您已经注意到的,
elount
无法使用此设置:

public function health_profiles() ...

$user = User::find($id);
$user->health_profiles; // null
因为在加载动态属性(关系)时,它会在模型上查找camelCased方法

但是,快速加载将按预期工作:

$user = User::with('health_profiles')->find($id);
$user->health_profiles; // related model/collection
所以,如果你想成为你的朋友,你一定要遵守命名规则


但这还不是全部。它将以另一种方式工作:

public function healthProfiles() ...

$user = User::find($id);
$user->healthProfiles; // works, returns related model/collection
$user->health_profiles; // works as well, returns the model/collection

总结并回答您的问题,以下每一项都适用于您:

// Assuming Profile is the model

// 2 queries
$user = User::find($id);
$profiles = $user->healthProfiles;

// or 1 query
$profiles = Profile::where('user_id', $id)->get();

// or 2 queries: 1 + 1 subquery
$profiles = Profile::whereHas('user', function ($q) use ($id) {
   $q->where('users.id', $id);
})->get();


可爱,好看。。。而且,您不需要在单个模型上使用急切加载,因此我很高兴看到您将其删除。:)可爱,好看。。。而且,您不需要在单个模型上使用急切加载,因此我很高兴看到您将其删除。:)谢谢这个方法也有效!但问题是,它还返回用户属性以及HealthProfiles,这对于我的需要来说太多了。实际上你是对的,如果你先链接,它会返回错误的用户id记录。请尝试
$User->healthProfiles
。这将缩小返回数据的范围,使其仅限于健康档案Hey man!我就是这么做的!无论如何,我如何筛选我的healthProfiles以只返回“parent_id”=null的记录您可以尝试
$user->health_profiles()->where('parent_id','=','null')谢谢!这个方法也有效!但问题是,它还返回用户属性以及HealthProfiles,这对于我的需要来说太多了。实际上你是对的,如果你先链接,它会返回错误的用户id记录。请尝试
$User->healthProfiles
。这将缩小返回数据的范围,使其仅限于健康档案Hey man!我就是这么做的!无论如何,我如何筛选我的healthProfiles以只返回“parent_id”=null的记录您可以尝试
$user->health_profiles()->where('parent_id','=','null')谢谢!这也是可行的,但我只需要health_概要文件数据,这样做还将返回用户模型数据,这对我的方法来说是一种过分的杀伤力。谢谢如果你愿意,我可以向你展示一种从另一个方向实现的方法。请分享。这是我的方法$user=user::其中('id','=',$id)->first()$用户->健康档案->toArray();如果你看deczo的答案,最后一个例子是我要建议的另一种方式。谢谢!这也是可行的,但我只需要health_概要文件数据,这样做还将返回用户模型数据,这对我的方法来说是一种过分的杀伤力。谢谢如果你愿意,我可以向你展示一种从另一个方向实现的方法。请分享。这是我的方法$user=user::其中('id','=',$id)->first()$用户->健康档案->toArray();如果你看看deczo的答案,最后一个例子是我建议的另一种方式。谢谢@deczo的详细解释!感谢@deczo的详细解释!