Get total permissions from users & roles belongsToMany












0















I have 3 models:




  • User

  • Role

  • Permission


Note:




  • a user can have direct permissions

  • a role can have permissions

  • a user can have roles


I'm trying to get the total permissions a user has, either through their direct permissions or through their roles permissions. So I need to combine both into 1 collection and count the total.



I've set up the belongsToMany relationships for User and Role:



public function permissions()
{
return $this->belongsToMany('AppPermission');
}


How do I do this?










share|improve this question



























    0















    I have 3 models:




    • User

    • Role

    • Permission


    Note:




    • a user can have direct permissions

    • a role can have permissions

    • a user can have roles


    I'm trying to get the total permissions a user has, either through their direct permissions or through their roles permissions. So I need to combine both into 1 collection and count the total.



    I've set up the belongsToMany relationships for User and Role:



    public function permissions()
    {
    return $this->belongsToMany('AppPermission');
    }


    How do I do this?










    share|improve this question

























      0












      0








      0








      I have 3 models:




      • User

      • Role

      • Permission


      Note:




      • a user can have direct permissions

      • a role can have permissions

      • a user can have roles


      I'm trying to get the total permissions a user has, either through their direct permissions or through their roles permissions. So I need to combine both into 1 collection and count the total.



      I've set up the belongsToMany relationships for User and Role:



      public function permissions()
      {
      return $this->belongsToMany('AppPermission');
      }


      How do I do this?










      share|improve this question














      I have 3 models:




      • User

      • Role

      • Permission


      Note:




      • a user can have direct permissions

      • a role can have permissions

      • a user can have roles


      I'm trying to get the total permissions a user has, either through their direct permissions or through their roles permissions. So I need to combine both into 1 collection and count the total.



      I've set up the belongsToMany relationships for User and Role:



      public function permissions()
      {
      return $this->belongsToMany('AppPermission');
      }


      How do I do this?







      php laravel eloquent relationship






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 6:11









      Kevin DionKevin Dion

      596




      596
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I figured it out via flatMap.






          share|improve this answer































            0














            You need to use the hasManyThrough relation



            Here is the link to the documentation: Eloquent Documentation



            so you would do something like this:



            public function permissions()
            {
            $directPermissions = $this->belongsToMany('AppPermission');
            $rolePermissions = $this->hasManyThrough('AppPermissions', 'AppRole');
            return $directPermissions->merge($rolePermissions);
            }





            share|improve this answer
























            • Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

              – Kevin Dion
              Nov 24 '18 at 6:33





















            0














            i am not sure but it will help
            open your user model



            public function roles()
            {
            return $this->belongsToMany(Role::class);
            }

            public function permissions()
            {
            return $this->belongsToMany(Permission::class);
            }

            public function hasRole(...$roles)
            {

            foreach($roles as $role)
            {
            if($this->roles->contains('name',$role))
            {
            return true;
            }
            }
            return false;
            }

            public function hasPermission($permission)
            {
            return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
            }

            public function hasPermissionThroughRole($permission)
            {
            foreach($permission->roles as $role)
            {
            if($this->roles->contains($role))
            {
            return true;
            }
            }
            return false;
            }


            Then open your Role Model and add these



            public function permissions()
            {
            return $this->belongsToMany(Permission::class);
            }

            public function users()
            {
            return $this->belongsToMany(User::class);
            }


            and open your permission model



            public function roles()
            {
            return $this->belongsToMany(Role::class);
            }

            public function users()
            {
            return $this->belongsToMany(User::class);
            }


            and finaly to boot all the permission to roles and user
            run the command php artisan make:provider PermissionServiceProvider



            open you service provider created newely and



            add



            use AppPermission;
            use IlluminateSupportFacadesGate;


            add the code under the boot method



            Permission::get()->map(function ($permission) 
            {
            Gate::define($permission->name, function ($user) use ($permission)
            {
            return $user->hasPermission($permission);
            });
            });


            hope it helps if you find any difficulties please comment below






            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%2f53455651%2fget-total-permissions-from-users-roles-belongstomany%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I figured it out via flatMap.






              share|improve this answer




























                0














                I figured it out via flatMap.






                share|improve this answer


























                  0












                  0








                  0







                  I figured it out via flatMap.






                  share|improve this answer













                  I figured it out via flatMap.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 6:23









                  Kevin DionKevin Dion

                  596




                  596

























                      0














                      You need to use the hasManyThrough relation



                      Here is the link to the documentation: Eloquent Documentation



                      so you would do something like this:



                      public function permissions()
                      {
                      $directPermissions = $this->belongsToMany('AppPermission');
                      $rolePermissions = $this->hasManyThrough('AppPermissions', 'AppRole');
                      return $directPermissions->merge($rolePermissions);
                      }





                      share|improve this answer
























                      • Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                        – Kevin Dion
                        Nov 24 '18 at 6:33


















                      0














                      You need to use the hasManyThrough relation



                      Here is the link to the documentation: Eloquent Documentation



                      so you would do something like this:



                      public function permissions()
                      {
                      $directPermissions = $this->belongsToMany('AppPermission');
                      $rolePermissions = $this->hasManyThrough('AppPermissions', 'AppRole');
                      return $directPermissions->merge($rolePermissions);
                      }





                      share|improve this answer
























                      • Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                        – Kevin Dion
                        Nov 24 '18 at 6:33
















                      0












                      0








                      0







                      You need to use the hasManyThrough relation



                      Here is the link to the documentation: Eloquent Documentation



                      so you would do something like this:



                      public function permissions()
                      {
                      $directPermissions = $this->belongsToMany('AppPermission');
                      $rolePermissions = $this->hasManyThrough('AppPermissions', 'AppRole');
                      return $directPermissions->merge($rolePermissions);
                      }





                      share|improve this answer













                      You need to use the hasManyThrough relation



                      Here is the link to the documentation: Eloquent Documentation



                      so you would do something like this:



                      public function permissions()
                      {
                      $directPermissions = $this->belongsToMany('AppPermission');
                      $rolePermissions = $this->hasManyThrough('AppPermissions', 'AppRole');
                      return $directPermissions->merge($rolePermissions);
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 '18 at 6:27









                      Edwin KrauseEdwin Krause

                      1,08111122




                      1,08111122













                      • Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                        – Kevin Dion
                        Nov 24 '18 at 6:33





















                      • Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                        – Kevin Dion
                        Nov 24 '18 at 6:33



















                      Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                      – Kevin Dion
                      Nov 24 '18 at 6:33







                      Nah I figured it out: $user->permissions->merge($user->roles->flatMap(function ($role) { return $role->permissions; }));

                      – Kevin Dion
                      Nov 24 '18 at 6:33













                      0














                      i am not sure but it will help
                      open your user model



                      public function roles()
                      {
                      return $this->belongsToMany(Role::class);
                      }

                      public function permissions()
                      {
                      return $this->belongsToMany(Permission::class);
                      }

                      public function hasRole(...$roles)
                      {

                      foreach($roles as $role)
                      {
                      if($this->roles->contains('name',$role))
                      {
                      return true;
                      }
                      }
                      return false;
                      }

                      public function hasPermission($permission)
                      {
                      return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
                      }

                      public function hasPermissionThroughRole($permission)
                      {
                      foreach($permission->roles as $role)
                      {
                      if($this->roles->contains($role))
                      {
                      return true;
                      }
                      }
                      return false;
                      }


                      Then open your Role Model and add these



                      public function permissions()
                      {
                      return $this->belongsToMany(Permission::class);
                      }

                      public function users()
                      {
                      return $this->belongsToMany(User::class);
                      }


                      and open your permission model



                      public function roles()
                      {
                      return $this->belongsToMany(Role::class);
                      }

                      public function users()
                      {
                      return $this->belongsToMany(User::class);
                      }


                      and finaly to boot all the permission to roles and user
                      run the command php artisan make:provider PermissionServiceProvider



                      open you service provider created newely and



                      add



                      use AppPermission;
                      use IlluminateSupportFacadesGate;


                      add the code under the boot method



                      Permission::get()->map(function ($permission) 
                      {
                      Gate::define($permission->name, function ($user) use ($permission)
                      {
                      return $user->hasPermission($permission);
                      });
                      });


                      hope it helps if you find any difficulties please comment below






                      share|improve this answer




























                        0














                        i am not sure but it will help
                        open your user model



                        public function roles()
                        {
                        return $this->belongsToMany(Role::class);
                        }

                        public function permissions()
                        {
                        return $this->belongsToMany(Permission::class);
                        }

                        public function hasRole(...$roles)
                        {

                        foreach($roles as $role)
                        {
                        if($this->roles->contains('name',$role))
                        {
                        return true;
                        }
                        }
                        return false;
                        }

                        public function hasPermission($permission)
                        {
                        return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
                        }

                        public function hasPermissionThroughRole($permission)
                        {
                        foreach($permission->roles as $role)
                        {
                        if($this->roles->contains($role))
                        {
                        return true;
                        }
                        }
                        return false;
                        }


                        Then open your Role Model and add these



                        public function permissions()
                        {
                        return $this->belongsToMany(Permission::class);
                        }

                        public function users()
                        {
                        return $this->belongsToMany(User::class);
                        }


                        and open your permission model



                        public function roles()
                        {
                        return $this->belongsToMany(Role::class);
                        }

                        public function users()
                        {
                        return $this->belongsToMany(User::class);
                        }


                        and finaly to boot all the permission to roles and user
                        run the command php artisan make:provider PermissionServiceProvider



                        open you service provider created newely and



                        add



                        use AppPermission;
                        use IlluminateSupportFacadesGate;


                        add the code under the boot method



                        Permission::get()->map(function ($permission) 
                        {
                        Gate::define($permission->name, function ($user) use ($permission)
                        {
                        return $user->hasPermission($permission);
                        });
                        });


                        hope it helps if you find any difficulties please comment below






                        share|improve this answer


























                          0












                          0








                          0







                          i am not sure but it will help
                          open your user model



                          public function roles()
                          {
                          return $this->belongsToMany(Role::class);
                          }

                          public function permissions()
                          {
                          return $this->belongsToMany(Permission::class);
                          }

                          public function hasRole(...$roles)
                          {

                          foreach($roles as $role)
                          {
                          if($this->roles->contains('name',$role))
                          {
                          return true;
                          }
                          }
                          return false;
                          }

                          public function hasPermission($permission)
                          {
                          return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
                          }

                          public function hasPermissionThroughRole($permission)
                          {
                          foreach($permission->roles as $role)
                          {
                          if($this->roles->contains($role))
                          {
                          return true;
                          }
                          }
                          return false;
                          }


                          Then open your Role Model and add these



                          public function permissions()
                          {
                          return $this->belongsToMany(Permission::class);
                          }

                          public function users()
                          {
                          return $this->belongsToMany(User::class);
                          }


                          and open your permission model



                          public function roles()
                          {
                          return $this->belongsToMany(Role::class);
                          }

                          public function users()
                          {
                          return $this->belongsToMany(User::class);
                          }


                          and finaly to boot all the permission to roles and user
                          run the command php artisan make:provider PermissionServiceProvider



                          open you service provider created newely and



                          add



                          use AppPermission;
                          use IlluminateSupportFacadesGate;


                          add the code under the boot method



                          Permission::get()->map(function ($permission) 
                          {
                          Gate::define($permission->name, function ($user) use ($permission)
                          {
                          return $user->hasPermission($permission);
                          });
                          });


                          hope it helps if you find any difficulties please comment below






                          share|improve this answer













                          i am not sure but it will help
                          open your user model



                          public function roles()
                          {
                          return $this->belongsToMany(Role::class);
                          }

                          public function permissions()
                          {
                          return $this->belongsToMany(Permission::class);
                          }

                          public function hasRole(...$roles)
                          {

                          foreach($roles as $role)
                          {
                          if($this->roles->contains('name',$role))
                          {
                          return true;
                          }
                          }
                          return false;
                          }

                          public function hasPermission($permission)
                          {
                          return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
                          }

                          public function hasPermissionThroughRole($permission)
                          {
                          foreach($permission->roles as $role)
                          {
                          if($this->roles->contains($role))
                          {
                          return true;
                          }
                          }
                          return false;
                          }


                          Then open your Role Model and add these



                          public function permissions()
                          {
                          return $this->belongsToMany(Permission::class);
                          }

                          public function users()
                          {
                          return $this->belongsToMany(User::class);
                          }


                          and open your permission model



                          public function roles()
                          {
                          return $this->belongsToMany(Role::class);
                          }

                          public function users()
                          {
                          return $this->belongsToMany(User::class);
                          }


                          and finaly to boot all the permission to roles and user
                          run the command php artisan make:provider PermissionServiceProvider



                          open you service provider created newely and



                          add



                          use AppPermission;
                          use IlluminateSupportFacadesGate;


                          add the code under the boot method



                          Permission::get()->map(function ($permission) 
                          {
                          Gate::define($permission->name, function ($user) use ($permission)
                          {
                          return $user->hasPermission($permission);
                          });
                          });


                          hope it helps if you find any difficulties please comment below







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 10:01









                          Manojkiran.AManojkiran.A

                          33028




                          33028






























                              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%2f53455651%2fget-total-permissions-from-users-roles-belongstomany%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

                              Refactoring coordinates for Minecraft Pi buildings written in Python