The current query is filtering Facturas by the vendedor_id field, but it's not taking into account the nullable relationship with Clientes. One solution could be to use a left join to include the nullable relationship and filter by both vendedor_id fields. Here's an example:
$qry = Facturas::leftJoin('clientes', 'facturas.cliente_id', '=', 'clientes.id')
->where('facturas.tipo', 'I')
->where(function($q) use ($filtro, $usr) {
$q->where('facturas.vendedor_id', $usr->id)
->orWhere('clientes.vendedor_id', $usr->id);
})
->where(function($q) use ($filtro) {
$q->where('clientes.nombreComercial', 'like', "%$filtro%")
->orWhere('clientes.razonSocial', 'like', "%$filtro%");
})
->orderByDesc('facturas.fecha')
->paginate(150);
This query uses a left join to include the nullable relationship with Clientes, and then filters by both vendedor_id fields using an OR condition. It also includes the filter applied to the nombreComercial and razonSocial fields of Clientes.