0

This is my query:

$data=Auth::user()->roles()->with('permissions')->get()->pluck('permissions'); return $data;

The output of the query is as following:

 [
  [
   {
    id: 2,
    name: "Read",
    slug: "Read",
    description: "This is Simple Read Permission",
     pivot: {
     role_id: 2,
     permission_id: 2,
     },
    },
   {
    id: 3,
    name: "Update",
    slug: "Update",
    description: "This is Simple Update Permission",

    pivot: {
     role_id: 2,
     permission_id: 3,
    },
   },
  ]
 ]

Now I want to convert this into an array. I've already use the map() and toArray() function, but the result still remains the same format.

How can I convert this collection in to an array?

Gabrielle-M
  • 1,037
  • 4
  • 17
  • 39
  • 1
    What about `->all()` ? [Laravel doc](https://laravel.com/docs/5.7/collections#method-all) – Levente Otta Jan 07 '19 at 18:45
  • Just a guess here, but I'd have the pluck('permissions') before the actual get() – Ole Haugset Jan 07 '19 at 18:54
  • I've use the `pluck` before `get()` but it's shows error.. @OleHaugset – Gabrielle-M Jan 07 '19 at 19:02
  • @Gabrielle All the permissions of the user is what you really need right? If so do you use a package? – Hilmi Erdem KEREN Jan 07 '19 at 19:50
  • Do you want every single element in the objects to be an array? You will have to convert it recursively in this case. Here is a [gist](https://gist.github.com/victorbstan/744478) – Mike Harrison Jan 07 '19 at 21:15
  • When you return an array from a route/controller method in Laravel it will automatically convert it to JSON. The example in your question is just the JSON representation of the array. – Rwd Jan 07 '19 at 21:18
  • @RossWilson I think the OP wants it to be a native array, not an object. That way you can use array accessors instead of object properties. – Mike Harrison Jan 07 '19 at 21:25
  • 1
    Possible duplicate of [laravel collection to array](https://stackoverflow.com/questions/35284974/laravel-collection-to-array) – Hilmi Erdem KEREN Jan 07 '19 at 21:53
  • @Gabrielle Can you tell us what you want to do with the array e.g. is it a response to an ajax request or an api call, or are you going to pass it to a view/blade file? As mentioned in an earlier comment, returning just an array/object/collection from a Route/controller method (used for the route) it will be converted to a JSON response, however, if you're passing it to a blade file it won't be. We just need to know a bit more about what you're planning to do to be able to help you :) – Rwd Jan 07 '19 at 22:01

0 Answers0