php - Laravel 5.2 - Change Data Format get from Eloquent -


i have model this-

$feature_project = featureproject::select('feature_id')                        ->where('project_id', $project->id)                        ->get(); 

and if return it, getting output this-

[   {     "feature_id": 2   },   {     "feature_id": 4   },   {     "feature_id": 9   } ] 

but want t output this-

[2,4,9] 

so need convert output.

but not finding way without using for-each loop (make temp array, push elements array current array for-each loop).

but think there more smart way in laravel that.

i think laravel collection used purpose.

you can call pluck() method on query builder.

$feature_project = featureproject::select('feature_id')                                         ->where('project_id', $project->id)                                         ->pluck('feature_id'); // [2,4,9] 

https://laravel.com/api/5.2/illuminate/database/eloquent/builder.html#method_lists

alternatively, can use php's array_column() function raw arrays. http://php.net/manual/en/function.array-column.php


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -