简体   繁体   中英

Laravel PHP get values from specific column

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"] }

Try using pluck method instead of select

where(...)->pluck('column2');

You can do

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

the method toArray() will convert the collection to array

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