Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

harini_ravi4's avatar

I used Eloquent query to extract the below table

$ user = User::all();

id q1    q2   q3    q4     q5   q6    q7   
0  0.01  0.8  null  0.9   null  0.9   0.1
1  0.8   null null  1     null   1    null
2  null  null null  0.03  null  0.03  null
3  0.04  0.4  null  0.9   null  0.5   null
4  null  0.67 null  0.8   null    9   0.8
5  0.07  0.9  null  0.6   null   10   null
6   1    null null   1    null  0.05  null

The final table should look like this

id q1    q2      q4      q6    q7   
0  0.01  0.8    0.9     0.9   0.1
1  0.8   null   1        1    null
2  null  null   0.03    0.03  null
3  0.04  0.4    0.9     0.5   null
4  null  0.67   0.8       9   0.8
5  0.07  0.9    0.6      10   null
6   1    null    1      0.05  null

I tried the query with orWhereNotNull and WhereNotNull but nothing works

$user = view_dumpmodelcsv_view::where(function($q){
         return $q->orWhereNull('q1')
       ->orWhereNotNull('q2')
       ->orWhereNotNull('q3')
       ->orWhereNotNull('q4')
       ->orWhereNotNull('q5')
       ->orWhereNotNull('q6')
       ->orWhereNotNull('q7');
})->get();
0 likes
2 replies
vincent15000's avatar

That's strange that you have an id equal to zero in the database.

Ok so you need to hide a field only if all its values for all lines are null.

According to me, it's not possible to do that because your where clauses are only applied to eacu model separately.

What I would do is to check over all the lines for each field in the table and if a field is null for all the lines, you don't show it.

Perhaps something like this.

use Illuminate\Support\Facades\Schema;
...
$columns = return Schema::getColumnListing('my_super_table');
$columns_to_show = [];
foreach ($columns as $column) {
	if (view_dumpmodelcsv_view::whereNull($column)->count() > 0) {
		$columns_to_show[] = $column;
	}
}
$user = view_dumpmodelcsv_view::select($columns_to_show)->get();

Please or to participate in this conversation.