Laravel 在 all() 之后通过 pivot on load() 从关系中获取列的值

Laravel get value of column from relation through pivot on load() after all()

我在从 Laravel 中的关系中检索列的值时遇到问题。
我有一个用户模型。该模型与 table 顺便说一句有关。名为 Userhobbies 的模型。
现在我们有:
User ::: hasMany >>> Userhobbies
现在
User::all()->load('hobbies')
我得到了正确的结果,比如

{"id":"1","username":"jdoe","first_name":"Joe","last_name":"Doe","birth":"
1992-04-11","picture_id":"f3dca65323e876026b409b9ba3d49c56","hobbies":
[{"hobby_id":"1","user_id":"1"},{"hobby_id":"2","user_id":"1"},
{"hobby_id":"3","user_id":"1"},{"hobby_id":"4","user_id":"1"}]}

如您所见,Userhobbies 仅包含爱好 - table(爱好模型)和用户 - table(用户模型)之间的主键关系。
(爱好模型与用户爱好也有 hasMany 关系)

我现在的问题是 - 如何在我的调用中(在 load('hobbies') 之后)检索所有爱好名称(来自爱好 - table)并且是否可以不编写大量代码?

为了更好地理解我的想法,我想要检索的结果:

{"id":"1","username":"jdoe","first_name":"Joe","last_name":"Doe","birth":"
1992-04-11","picture_id":"f3dca65323e876026b409b9ba3d49c56","hobbies":
["golf", "cards", "games", "football"]}


编辑:
如果我尝试以下(我尝试在用户和爱好中使用 belongsToMany):

User::with('hobbies')->get()->first()

我从爱好中获得了全部价值 - table:

{user-specific data ...
hobbies:[{"id":"1","name":"golf","created_at":"2015-04-07 
14:15:02","updated_at":"2015-04-07 14:15:02","pivot":
{"user_id":"1","hobby_id":"1"}},
{"id":"2","name":"cards","created_at":"2015-04-07 
14:15:02","updated_at":"2015-04-07 14:15:02","pivot":    
{"user_id":"1","hobby_id":"2"}},
{"id":"3","name":"games","created_at":"2015-04-07 
14:15:02","updated_at":"2015-04-07 14:15:02","pivot":
{"user_id":"1","hobby_id":"3"}},
{"id":"4","name":"football","created_at":"2015-04-07 
14:15:02","updated_at":"2015-04-07 14:15:02","pivot":
{"user_id":"1","hobby_id":"4"}}]}

同样尝试 ->load('hobbies')。我真的不知道如何继续下去。

为了更详细地解释一下我需要什么,可以想象这样的查询:

User::all(['id', 'name'])->load(array('hobbies.id','hobbies.name'))->get();

据我所知,我知道可以使用闭包对执行加载的查询设置约束,如下所示:

User::all()->load(['hobbies' => function($query)
{
    $query->select('id', 'name');
}]);

通过这样做,当你将它转换为数组时,它会产生一个接近你想要的结果。您甚至可以将 'pivot' 添加到您 Hobby 模型上的 $hidden 属性 以隐藏此信息。