closures - Laravel returning array from each() method -
I am getting an array of permissions related to the role of a specific user, mainly to get my head around working With eager loading and with each () method
In this way a $ array is making the array, so how can I use the array outside the closing?
$ User = user :: with ('roles.perms') - & gt; Search (1); $ Permissions = array (); $ List = $ user- & gt; Roles-gt; Each (function ($ role) usage ($ permissions) {$ permission = array_merge ($ permissions, $ role-> perms- & gt; toArray ());});
Thanks!
Just enter the $ permissions by
variable:
$ user- & gt; Roles-gt; Usage of each (function ($ role) (& amp; $ permission) {// - Reference passed $ $ permission = array_merge ($ permissions, $ role-> perms- & gt; toArray) );}); Note , and
, you now have the same value declared outside of the function and it is in your current radius and is modified within close time. Therefore, you can use $ permissions
here, for example:
dd ($ permissions); // dump and die
Comments
Post a Comment