Skip to content
Advertisement

Laravel: How to separate array from query result into variables?

I have something like this:

return Model::get()->pluck('count','column');

Result:
{"ABC":1,"DEF":3,"GHI":1,"JKL":2}

How to separate the ‘column’ and ‘count’ into variables?

$column = ["ABC","DEF","GHI","JKL"];

$count = [1,3,1,2];

Advertisement

Answer

Use native PHP functions; array_keys and array_values.

$result = Model::get()->pluck('count', 'column');
$column = array_keys($result);
$count = array_values($result);

For more details, read the below links;

https://www.php.net/manual/en/function.array-keys.php

https://www.php.net/manual/en/function.array-values.php

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement