0

I am using an Eloquent Model.
Example table:

column_1 | column_2 | column_3
---------|----------|---------
data_1   | data_2   | 1
---------|----------|---------
data_4   | data_5   | 1
---------|----------|---------
data_7   | data_8   | 0

I would like to get every value from column_2 where column_3 is 1.
So in this case: data_2 and data_5

I tired where(...)->select('column_2')->get()->toArray() but it returns the column name too:
[{"column_2":"data_2"},{"column_2":"data_5"}]

I need a simple array returned like this:
[data_2, data_5] or { "name": ["data_2", "data_5"] }

2 Answers 2

2

Try using pluck method instead of select

where(...)->pluck('column2');
Sign up to request clarification or add additional context in comments.

Comments

0

You can do

Model::where('column_3', 1)->pluck('column_2')->toArray();

the method toArray() will convert the collection to array

Comments

Your Answer

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