Is it possible to do the following in Laravel 4? ...
DB::table('myTable')
->select(DB::raw($columnNames))
->whereNotNull(function($query) use($columns) {
foreach ($columns as $column) {
$query->whereNotNull($column);
}
})
->get();
If I have the following table:
table: myTable
id | name | age | weight
======================================
1 Jane NULL 150
2 NULL 12 80
3 Bob NULL NULL
4 John 22 120
5 Cody NULL NULL
If $columns
is [age, weight]
and $columnNames
is 'age, weight'
, then applying the above whereNotNull statement, I would expect output like this:
age | weight
===================
NULL 150
12 80
22 120
How can I get this done?
UPDATE:
The condition is to return all rows where the selected columns are not ALL null. So a whereNotNull
clause must be applied to each (selected) column in each row. If all columns are NULL, then whereNotNull will return false and that row shouldn't be part of the results. So only rows which have AT LEAST one non-NULL value should be returned.
$columns
can't beNULL
? – Precisian