Jonuux Jonuux - 1 year ago 161
PHP Question

Laravel get single column result as string

Hello I would like to know is it possible to get column value as string. Instead of array in array:

Current query:


The result at the moment is this:

Array in array

Preferable result:

String instead of array

Answer Source

Before your toArray() call, add pluck('number'):

$result = Number::limit('1000')->get(['number'])->pluck('number')->toArray();

That's it! This will pluck just the number attributes from your result collection, and give you a single-level array.

The reason this works, is because you are getting a Collection back from get():

All multi-result sets returned by Eloquent are an instance of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship.

And the pluck method:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download