Laravel 4 - Get Array of Attributes from Collection

后端 未结 4 1868
长发绾君心
长发绾君心 2021-02-01 14:35

I have a collection of objects. Let\'s say the objects are tags:

$tags = Tag::all();

I want to retrieve a certain attribute for each tag, say i

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 14:42

    You could use array_column for this (it's a PHP 5.5 function but Laravel has a helper function that replicates the behavior, for the most part).

    Something like this should suffice.

    $tag_names = array_column($tags->toArray(), 'name');
    

提交回复
热议问题