I am new to laravel and i barely managed to do up until this point.
I need guidance on putting the data @dump on the organigram.
I really have no idea where to start this. Any direction or idea is appreciated.
There are users who are referred by others, and the actual result shows the hierarchy of the user, who is referred by who. For instance, you can see that user with id 27 has referred 3 other user under him, 35,36, 38, and 36 has referred an additional user with ID 37.
thank you
public function viewRelated()
{
$roles = collect(Role::whereIn('slug', array_merge(Role::FIRST_PARTY_ROLES, Role::SECOND_PARTY_ROLES, ['master']))->get())->keyBy('id')->toArray();
$users = User::whereIn('role', array_keys($roles))
->where('email_verified_at', '<>', null)
->where('account_verified_at', '<>', null)->get();
$users = $this->buildUserHierarchy($users);
return view('dashboard.admin.users.relationships', ['users' => $users]);
}
protected function buildUserHierarchy($users, $user_id = null, $level = 0)
{
$users_tree = [];
$parents = collect($users)->where("assigned_to", "==", $user_id)->values();
foreach ($parents as $child) {
$child->users = $this->buildUserHierarchy($users, $child->id, $level + 1);
$item = [
'id' => $child->id,
'name' => $child->name . ' ' . $child->surname,
'email' => $child->email,
'role' => Role::find($child->role)->name,
'users' => $this->buildUserHierarchy($users, $child->id, $level + 1),
];
$users_tree[$child->id] = $item;
}
return $users_tree;
}
Route::get('list', 'App\Http\Controllers\Dashboard\UserController@viewRelated')->name('user.list');
<div class="row">
<div>
@if($users)
@dump($users)
@endif
</div>
</div>