1"use strict";(self.webpackChunkroles_and_permissions=self.webpackChunkroles_and_permissions||[]).push([[313],{3905:function(e,t,n){n.d(t,{Zo:function(){return p},kt:function(){return d}});var o=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t){if(null==e)return{};var n,o,r=function(e,t){if(null==e)return{};var n,o,r={},l=Object.keys(e);for(o=0;o<l.length;o++)n=l[o],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(o=0;o<l.length;o++)n=l[o],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var s=o.createContext({}),c=function(e){var t=o.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},p=function(e){var t=c(e.components);return o.createElement(s.Provider,{value:t},e.children)},u={inlineCode:"code",wrapper:function(e){var t=e.children;return o.createElement(o.Fragment,{},t)}},m=o.forwardRef((function(e,t){var n=e.components,r=e.mdxType,l=e.originalType,s=e.parentName,p=a(e,["components","mdxType","originalType","parentName"]),m=c(n),d=r,h=m["".concat(s,".").concat(d)]||m[d]||u[d]||l;return n?o.createElement(h,i(i({ref:t},p),{},{components:n})):o.createElement(h,i({ref:t},p))}));function d(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var l=n.length,i=new Array(l);i[0]=m;var a={};for(var s in t)hasOwnProperty.call(t,s)&&(a[s]=t[s]);a.originalType=e,a.mdxType="string"==typeof e?e:r,i[1]=a;for(var c=2;c<l;c++)i[c]=n[c];return o.createElement.apply(null,i)}return o.createElement.apply(null,n)}m.displayName="MDXCreateElement"},4345:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return a},contentTitle:function(){return s},metadata:function(){return c},toc:function(){return p},default:function(){return m}});var o=n(7462),r=n(3366),l=(n(7294),n(3905)),i=["components"],a={title:"Role collection",sidebar_position:1},s=void 0,c={unversionedId:"collections/role-collection",id:"collections/role-collection",title:"Role collection",description:"Overview",source:"@site/docs/collections/role-collection.md",sourceDirName:"collections",slug:"/collections/role-collection",permalink:"/docs/collections/role-collection",editUrl:"https://github.com/ajimoti/roles-and-permissions/docs/collections/role-collection.md",tags:[],version:"current",sidebarPosition:1,frontMatter:{title:"Role collection",sidebar_position:1},sidebar:"tutorialSidebar",previous:{title:"Roles in Hierarchy",permalink:"/docs/enums/hierarchy"},next:{title:"Permission collection",permalink:"/docs/collections/permission-collection"}},p=[{value:"Overview",id:"overview",children:[],level:2},{value:"Explanation",id:"explanation",children:[{value:"getPermissions(): PermissionCollection",id:"getpermissions-permissioncollection",children:[],level:3}],level:2},{value:"Customizing behaviors",id:"customizing-behaviors",children:[{value:"toArray()",id:"toarray",children:[],level:3}],level:2}],u={toc:p};function m(e){var t=e.components,n=(0,r.Z)(e,i);return(0,l.kt)("wrapper",(0,o.Z)({},u,n,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("h2",{id:"overview"},"Overview"),(0,l.kt)("p",null,"A role collection is a fluent, and convenient wrapper for working with the role instances. "),(0,l.kt)("p",null,"Most times you will find yourself working with the ",(0,l.kt)("inlineCode",{parentName:"p"},"Ajimoti\\RolesAndPermissions\\Collections\\RoleCollection")," class. This class is an extension of laravel ",(0,l.kt)("inlineCode",{parentName:"p"},"Illuminate\\Support\\Collection"),", and allows you chain laravel's collection methods to filter or alter the values."),(0,l.kt)("div",{className:"admonition admonition-tip alert alert--success"},(0,l.kt)("div",{parentName:"div",className:"admonition-heading"},(0,l.kt)("h5",{parentName:"div"},(0,l.kt)("span",{parentName:"h5",className:"admonition-icon"}
1,(0,l.kt)("svg",{parentName:"span",xmlns:"http://www.w3.org/2000/svg",width:"12",height:"16",viewBox:"0 0 12 16"},(0,l.kt)("path",{parentName:"svg",fillRule:"evenodd",d:"M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"}))),"tip")),(0,l.kt)("div",{parentName:"div",className:"admonition-content"},(0,l.kt)("p",{parentName:"div"},"New to laravel collections? You can read up on how to use ",(0,l.kt)("a",{parentName:"p",href:"https://laravel.com/docs/8.x/collections"},"collections here")))),(0,l.kt)("h2",{id:"explanation"},"Explanation"),(0,l.kt)("p",null,"Usually, the role collection contains an array of the resulting role instances.\nThe role collection also allows you chain laravel's collection methods to filter to alter the values."),(0,l.kt)("p",null,(0,l.kt)("strong",{parentName:"p"},"For example")," "),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-php"},"$roles = Role::all();\n\n$roles->pluck('description'); // returns an array of all descriptions\n")),(0,l.kt)("p",null,"You can see the ",(0,l.kt)("a",{parentName:"p",href:"https://laravel.com/docs/8.x/collections#available-methods"},"list of methods available on a collection here")," "),(0,l.kt)("h3",{id:"getpermissions-permissioncollection"},"getPermissions(): PermissionCollection"),(0,l.kt)("p",null,"You can fetch all the permissions in a collection. "),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-php"},"$roles = Role::all();\n\n$roles->getPermissions(); // returns a collection of all the permissions\n")),(0,l.kt)("h2",{id:"customizing-behaviors"},"Customizing behaviors"),(0,l.kt)("h3",{id:"toarray"},"toArray()"),(0,l.kt)("p",null,"This is one of the many methods made available on the role collection. This method is used to convert the role collection to an array. "),(0,l.kt)("p",null,"By default, when the ",(0,l.kt)("inlineCode",{parentName:"p"},"->toArray()")," method is chained to a role collection, an array of the role ",(0,l.kt)("inlineCode",{parentName:"p"},"value(s)")," will be returned in their native type. You may want to have access to the another property instead of the ",(0,l.kt)("inlineCode",{parentName:"p"},"value")," property (any of ",(0,l.kt)("inlineCode",{parentName:"p"},"permissions"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"key"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"description"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"title"),")."),(0,l.kt)("p",null,"To change this behavior, you can override the ",(0,l.kt)("inlineCode",{parentName:"p"},"toArray()")," method in your role enum."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-php",metastring:"title='app\\Enums\\Role.php'",title:"'app\\Enums\\Role.php'"},"// ---\n // will return the permissions when called\n public function toArray()\n {\n return $this->permissions;\n }\n// ---\n\n\n$roles = Role::all();\n$roles->toArray(); // returns an array of each role permissions\n")))}m.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.