Laravel 查询构建器调用成员函数 where() on null

Laravel query builder Call to a member function where() on null

我正在尝试进行验证以检查值是否为空。但是它在执行时显示错误。这是代码:

$validation_query = DB::table('immunizations')->whereNull('deleted_at');
                        
foreach ($fileheaders as $valkey => $valvalue) {
    foreach($valvalue as $vlrw=>$valrowval) {
        if($schema[$immunization_mapping[$vlrw]->field_id] == 'personnel_id'){
            $validation_query = $validation_query->where('personnel_id','=', $valrowval);
        }
        if($schema[$immunization_mapping[$vlrw]->field_id] == 'immunization_date'){
            $validation_query = $validation_query->where('immunization_date', '>=', $valrowval);
        }
        $validation_query = $validation_query->where('immunization_type_id', '=', $immunization_type_id);
        $validation_query = $validation_query->first();
        
        if ($validation_query != '' && $validation_query->id != '') {
            $err = 1;
        }
    }
    $err_array['duplication'][] = $valkey;
    $valcnt++;
}

这个循环有什么问题吗?

只需在 foreach 的每个循环中为您的查询提供一个合适的初始值即可:

 foreach ($fileheaders as $valkey => $valvalue) {
                    foreach($valvalue as $vlrw=>$valrowval) {

$validation_query = DB::table('immunizations')->whereNull('deleted_at');


if($schema[$immunization_mapping[$vlrw]->field_id] == 'personnel_id'){
                            $validation_query = $validation_query->where('personnel_id','=', $valrowval);
                        }
                        if($schema[$immunization_mapping[$vlrw]->field_id] == 'immunization_date'){
                            $validation_query = $validation_query->where('immunization_date', '>=', $valrowval);
                        }
                        $validation_query = $validation_query->where('immunization_type_id', '=', $immunization_type_id);
                        $validation_query = $validation_query->first();


                        if ($validation_query != '' && $validation_query->id != '') {
                            $err = 1;
                        }
                    }
                    $err_array['duplication'][] = $valkey;
                    $valcnt++;
                }