Pooja
Pooja

Reputation: 81

How to get column name from the table with particular value

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

Answers (2)

Mykhailo YATSYSHYN
Mykhailo YATSYSHYN

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

Alexey Mezenin
Alexey Mezenin

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

Related Questions