简体   繁体   中英

Result query to single array of string

I would like to transform the result of an Eloquent query into a single array of strings.

What my query currently gives: (I apply a ->toArray() to initial query to get that:)

array:2 [▼
  0 => array:1 [▼
    "title" => "User"
  ]
  1 => array:1 [▼
    "title" => "Premium"
  ]
]

What I would like it to give me:

['User', 'Premium'];

My query:

Rank::where('strength', '<', $this->rank->strength)->select('title')->get();

Thanks in advance

You can use pluck() :

Rank::where('strength', '<', $this->rank->strength)->pluck('title');

If you want exactly same array response instead of collection, You can add toArray() function at the last

$ranks = Rank::where('strength', '<', $this->rank->strength)
        ->pluck('title')->toArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM