Kylian
Kylian

Reputation: 131

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

Upvotes: 0

Views: 90

Answers (2)

Rahul Jat
Rahul Jat

Reputation: 706

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();

Upvotes: 1

Rwd
Rwd

Reputation: 35200

You can use pluck():

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

Upvotes: 1

Related Questions