Skip to content

Instantly share code, notes, and snippets.

@adbrsln
Created October 12, 2018 12:19
Show Gist options
  • Save adbrsln/bb834e89781a56c8b12375230c0f6750 to your computer and use it in GitHub Desktop.
Save adbrsln/bb834e89781a56c8b12375230c0f6750 to your computer and use it in GitHub Desktop.
UserController
$users = User::all();
$response = [];
foreach($users as $user) {
$userArray = $user->toArray();
$roles = $user->roles()->select(array('id', 'role_name'))->get();
$states = $user->states_val()->select(array('id', 'state_name'))->get();
$credits = $user->credits()->withPivot('amount')->get();
$companies = $user->companies()->get();
foreach($roles as $role) {
$userArray['roles'] = $role;
}
foreach($states as $state) {
$userArray['states'] = $state;
}
foreach($credits as $credit) {
$userArray['credits'] = $credit;
}
foreach($companies as $company) {
$userArray['companies'] = $company;
}
$response[] = $userArray;
}
return response()->json($response,200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment