0

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

2 Answers 2

1

You can use pluck():

Rank::where('strength', '<', $this->rank->strength)->pluck('title');
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.