如何修复“此语句中不存在属性[id]”

时间:2019-06-12 11:49:24

标签: php laravel laravel-5 laravel-5.5

我想从2个表中选择一个数据提供给所有用户,并将此数据合并到要返回的对象中 例     {     用户:{     ID:1,     名称:bla,     保存的卡:[          {id:1,name:test},          {id:2,名称:test2}     ]     },     {id:2,名称:bla1,     保存的卡:[      {id:1,name:test},      {id:2,名称:test2},      {id:3,名称:test3}     ]     }     }

 public function getalluser(Request $request)
    {
        $User_data = User::where('users.role', '=', 0)
            ->get();

        $count = count($User_data);
       for ($i = 0; $i < $count; $i++) {
            $json_data[] = [
                'user' => User::where('users.role', '=', 0)
                    ->where( 'users.id', $User_data->id[$i])
                    ->leftJoin('webrole', 'users.role', '=', 'webrole.id')
                    ->get(),
                'saved cards' => User::where('users.role', '=', 0)
                    ->where( 'credit_cards.user_id', $User_data->id[$i])
                    ->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
                    ->get()
            ];

        } 
        return response()->json($User_data);
    }

1 个答案:

答案 0 :(得分:0)

foreach($User_data as $data) {
        $json_data[] = [
            'user' => User::where('users.role', '=', 0)
                ->where( 'users.id', $data->id)
                ->leftJoin('webrole', 'users.role', '=', 'webrole.id')
                ->get(),
            'saved cards' => User::where('users.role', '=', 0)
                ->where( 'credit_cards.user_id', $data->id)
                ->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
                ->get()
        ];

    }