我正在使用此代码来获取数据。
$user = User::where('active', 1)->with(['spots:spot_name,spot_uid'])->get();
这真是太棒了!它获取关系并输出数据。
然而,数据看起来是这样的。
user_uid: 5
spots: {spot_name: 'backend'}
description: "Test user works in helpdesk"
department: "9"
它显示了我需要的所有数据,但我希望spot_name不在它自己的子json数组中,而只是让spot_name为点。像这样:
spots: "backend"
我知道这是因为它获得了2列,然后我隐藏了一列。
这是不可能的,还是我只是走错了路?任何信息都很好。
你可以
User::where('active', 1)->withCount(['spots as spot_name' => function ($q) {
$q->select('spot_name');
}]);
输出
{
...
spot_name: 'backend'
}
您可以使用集合格式化输出:
$user->map(function ($x) {
$arr = $x->toArray();
$arr['spots'] = $arr['spots'][0]['spot_name'];
return $arr;
});
然后是这个用户。