Reputation: 81
i have a table like :
.............................
id | Sun | Mon | Tue | Wed |
.............................
1 | 0 | 1 | 1 | 1 |
.............................
Now i am getting all the column name from this query:
DB::getSchemaBuilder()->getColumnListing('weak');
it returning me the array of all columns name
but i want those column name which have the 1 value how can i get those column which have 1 value in laravel..any anyone please help me related this ??
Upvotes: 1
Views: 121
Reputation: 179
I think you need to build a Custom query using the IF function
SELECT
(IF(table.Sun == 1) table.Sun,)
(IF(table.Mon == 1) table.Mon,)
......
table.id
FROM table
Upvotes: -1
Reputation: 163748
You can use array_keys()
with the second parameter:
$object = DB::table('weak')->first();
$columns = array_keys((array)$object, 1);
The result will be:
['Mon', 'Tue', 'Wed']
Upvotes: 3