michal michal - 1 month ago 6
JSON Question

Laravel json convert to array only number PHP

I have code:

return $getId = Permission::select('id')->get();


This code returned me:

[{"id":1},{"id":2},{"id":3},{"id":4}]


I want to query returned the records in the form of:

["1","2","3","4"]


How can I do this? I tried to use
json_decode
, but it's not work I also tried to use a foreach loop, but the result was the same. I'm doing this because I want to compare the two tables using
array_diff
, but still I have error:


array_diff(): Argument #1 is not an array


Argument 1
is variable
$getId
.

Answer

You want to use pluck() and toArray() methods:

return $getId = Permission::pluck('id')->toArray();