获取 laravel 集合中的空列

Get empty columns in laravel collection

我有这个 Laravel 集合,是从数据库查询中获得的:

$c = collect(\DB::connection('mysql_scipg')
     ->table('producao_email')
     ->where('cod_cliente', $cliente_id)
     ->where('lote',$lote)
     ->whereIn('cod_status',$statusCliente)
     ->select('dominio','lote','sv_email','cod_status')
     ->get());

我遇到的问题是,在 sv_email 不同于 "":

的情况下,从集合中获取数据

示例:

有填写sv_email的情况,也有填写sv_email=""的情况,sv email要么是with information要么是"",note不为null,是"";

在下图中,我展示了一个案例 sv_email = ""

使用命令:

dd ($c-> where ('sv_email', '')

collection where sv_email = ""

我测试了命令 dd($c->where('sv_email','SVV095')

collection where sv_email != ""

当我尝试获取 sv_email 时出现问题,如果它与 ""

不同

我测试了:


    dd($c->where('sv_email','<>',"");
    dd($c->where('sv_email','<>','');
    dd($c->where('sv_email','!=',"");
    dd($c->where('sv_email','!=','');

全部返回一个空集合。

有returns如我所愿的情况。但是我必须再做一次查询,我不想这样,因为我必须在数据库中做2次查询才能获取数据,而且我可以操作集合,我哪里错了?

它起作用的情况是:


    $c_2 = collect(\DB::connection('mysql_scipg')
           ->table('producao_email')
           ->where('cod_cliente', $cliente_id)
           ->where('lote',$lote)
           ->where('sv_email','!=','')
           ->select('dominio','lote','sv_email','cod_status')
           ->get());

请试试这个方法:

$results = \DB::connection('mysql_scipg')
     ->table('producao_email')
     ->where('cod_cliente', $cliente_id)
     ->where('lote',$lote)
     ->whereIn('cod_status',$statusCliente)
     ->select('dominio','lote','sv_email','cod_status')
     ->get();

$filledEmailResults = $results->filter(function($value) {
    return !empty($value['sv_email']);
});

请注意 eloquent returns 集合中的 ->get() 方法,无需通过将结果传递给 collect() 方法将结果转换为集合