Recursive Relation Roles Laravel 5.6












0















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










share|improve this question

























  • Attach eloquent models with relations.

    – IndianCoding
    Nov 22 '18 at 9:22
















0















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










share|improve this question

























  • Attach eloquent models with relations.

    – IndianCoding
    Nov 22 '18 at 9:22














0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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






    share|improve this answer




























      1














      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






      share|improve this answer


























        1












        1








        1







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 11:36









        Adrian Hernandez-LopezAdrian Hernandez-Lopez

        318113




        318113






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'