Recursive Relation Roles Laravel 5.6
I just don't know how to query this things... I work in the collection but it's very awful code.
// user with roles
$user = User::where('id', Auth::User()->id)
->with('roles')
->first();
// Get the RoleParentId
$roleParentId = collect($user->roles)->min('parent_id');
// Get the all the children of the role parent Id with it's children and users
$role = AppModelRole::where('parent_id', $roleParentId)
->with('allChildren.users')
->first();
// Loop to get the Collection of children users
$childrenUsers = collect();
foreach($role->allChildren as $children){
$childrenUsers->push($children->users);
}
// Loop to get the Collection of user
$users = collect();
foreach($childrenUsers as $nCollect){
foreach($nCollect as $user){
$users->push($user);
}
}
return response()->json($users);
My goal is to get the users under a particular parent_id... I don't know how to query it so I used the collection but I think it's not a very good idea. TY
laravel recursion
add a comment |
I just don't know how to query this things... I work in the collection but it's very awful code.
// user with roles
$user = User::where('id', Auth::User()->id)
->with('roles')
->first();
// Get the RoleParentId
$roleParentId = collect($user->roles)->min('parent_id');
// Get the all the children of the role parent Id with it's children and users
$role = AppModelRole::where('parent_id', $roleParentId)
->with('allChildren.users')
->first();
// Loop to get the Collection of children users
$childrenUsers = collect();
foreach($role->allChildren as $children){
$childrenUsers->push($children->users);
}
// Loop to get the Collection of user
$users = collect();
foreach($childrenUsers as $nCollect){
foreach($nCollect as $user){
$users->push($user);
}
}
return response()->json($users);
My goal is to get the users under a particular parent_id... I don't know how to query it so I used the collection but I think it's not a very good idea. TY
laravel recursion
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22
add a comment |
I just don't know how to query this things... I work in the collection but it's very awful code.
// user with roles
$user = User::where('id', Auth::User()->id)
->with('roles')
->first();
// Get the RoleParentId
$roleParentId = collect($user->roles)->min('parent_id');
// Get the all the children of the role parent Id with it's children and users
$role = AppModelRole::where('parent_id', $roleParentId)
->with('allChildren.users')
->first();
// Loop to get the Collection of children users
$childrenUsers = collect();
foreach($role->allChildren as $children){
$childrenUsers->push($children->users);
}
// Loop to get the Collection of user
$users = collect();
foreach($childrenUsers as $nCollect){
foreach($nCollect as $user){
$users->push($user);
}
}
return response()->json($users);
My goal is to get the users under a particular parent_id... I don't know how to query it so I used the collection but I think it's not a very good idea. TY
laravel recursion
I just don't know how to query this things... I work in the collection but it's very awful code.
// user with roles
$user = User::where('id', Auth::User()->id)
->with('roles')
->first();
// Get the RoleParentId
$roleParentId = collect($user->roles)->min('parent_id');
// Get the all the children of the role parent Id with it's children and users
$role = AppModelRole::where('parent_id', $roleParentId)
->with('allChildren.users')
->first();
// Loop to get the Collection of children users
$childrenUsers = collect();
foreach($role->allChildren as $children){
$childrenUsers->push($children->users);
}
// Loop to get the Collection of user
$users = collect();
foreach($childrenUsers as $nCollect){
foreach($nCollect as $user){
$users->push($user);
}
}
return response()->json($users);
My goal is to get the users under a particular parent_id... I don't know how to query it so I used the collection but I think it's not a very good idea. TY
laravel recursion
laravel recursion
edited Nov 22 '18 at 9:35
Yoram de Langen
3,90511727
3,90511727
asked Nov 22 '18 at 9:19
RbexRbex
4021925
4021925
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22
add a comment |
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22
add a comment |
1 Answer
1
active
oldest
votes
You can use the whereHas
to get the expected result.
$users = User::whereHas('roles', function() {
//Get the roles where the parent_id is the (minimum) parent_id from the user roles
return $query->where('parent_id', Auth::user()->roles()->orderBy('parent_id', 'ASC')->first()->parent_id);
});
More info at https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427494%2frecursive-relation-roles-laravel-5-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the whereHas
to get the expected result.
$users = User::whereHas('roles', function() {
//Get the roles where the parent_id is the (minimum) parent_id from the user roles
return $query->where('parent_id', Auth::user()->roles()->orderBy('parent_id', 'ASC')->first()->parent_id);
});
More info at https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence
add a comment |
You can use the whereHas
to get the expected result.
$users = User::whereHas('roles', function() {
//Get the roles where the parent_id is the (minimum) parent_id from the user roles
return $query->where('parent_id', Auth::user()->roles()->orderBy('parent_id', 'ASC')->first()->parent_id);
});
More info at https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence
add a comment |
You can use the whereHas
to get the expected result.
$users = User::whereHas('roles', function() {
//Get the roles where the parent_id is the (minimum) parent_id from the user roles
return $query->where('parent_id', Auth::user()->roles()->orderBy('parent_id', 'ASC')->first()->parent_id);
});
More info at https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence
You can use the whereHas
to get the expected result.
$users = User::whereHas('roles', function() {
//Get the roles where the parent_id is the (minimum) parent_id from the user roles
return $query->where('parent_id', Auth::user()->roles()->orderBy('parent_id', 'ASC')->first()->parent_id);
});
More info at https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence
answered Nov 22 '18 at 11:36
Adrian Hernandez-LopezAdrian Hernandez-Lopez
318113
318113
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427494%2frecursive-relation-roles-laravel-5-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Attach eloquent models with relations.
– IndianCoding
Nov 22 '18 at 9:22