Home > Mobile >  I want to query from model of laravel to remove the columns that has only null values in laravel
I want to query from model of laravel to remove the columns that has only null values in laravel

Time:11-20

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 need to query the model in the laravel by removing the column that has only null values. Any help would be appreciated.

CodePudding user response:

Untested:

$q1 = $q2 = $q3 = $q4 = $q5 = $q6 = $q7 = false;

foreach($users as $user){
  if(!$q1){
    $q1 = $user->q1 ? true : false;
  }
  if(!$q2){
    $q2 = $user->q2 ? true : false;
  }
  if(!$q3){
    $q3 = $user->q3 ? true : false;
  }
  if(!$q4){
    $q4 = $user->q4 ? true : false;
  }
  if(!$q5){
    $q5 = $user->q5 ? true : false;
  }
  if(!$q6){
    $q6 = $user->q6 ? true : false;
  }
  if(!$q7){
    $q7 = $user->q7 ? true : false;
  }
}

at this point, you'll know what column is true or false. You will only display all columns that has true value.

Throw the $users, $q1, $q2, $q3, $q4, $q5, $q6, $q7 variables on blade files.

<table>

@foreach($users as $user)

<tr>
  @for ($i = 1; $i <= 7; $i  )
    @if($q$i)
      <td>
        {{ $user->q$i }}
      </td>
    @endif
  @endfor
</tr>
@endforeach

</table>

CodePudding user response:

This help bases on this idea: All null values will become only one null value after using array_unique([$array_of_null_values]).

So you can do like me as:

Set the content of Controller app\Http\Controllers\BasicController.php.

class BasicController extends Controller
{
    function process(Request $request){
        // $q1 = array_unique(User::pluck('q1')->toArray());
        // $q3 = array_unique(User::pluck('q3')->toArray());
        // dd($q1,$q3===array(null));
        $excludes = [];
        $includes = [];
        for ($i=1;$i<=7;$i  ){
            ${'q' . $i} = array_unique(User::pluck('q'. $i)->toArray());
            if(${'q' . $i}===array(null)) {
                $excludes [] = 'q' . $i;
            }else {
                $includes [] = 'q' . $i;
            }
        }
        $users = User::select(['id',...$includes])->get();
        return view('process', ['users'=>$users,'includes'=>$includes]);
    }
}

Reuse $includes from controller as blade resources\views\process.blade.php.

<body>
    <p>Users with some includes fields:</p>
    <table border="1">
        <tr>
            <th>id</th>
            @for($i=0;$i<count($includes);$i  )
                <th>{{ $includes[$i] }}</th>
            @endfor
        </tr>
        @foreach($users as $user)
        <tr>
            <td>{{ $user->id }}</td>
            @for($i=0;$i<count($includes);$i  )
                @if($user->{ $includes[$i] })
                    <td>{{ $user->{$includes[$i]} }}</td>
                @else
                    <td> null </td>
                @endif
            @endfor
        </tr>
        @endforeach
    </table>
</body>

And you will get desired result.

Final picture

  • Related