staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

How do I bring data only from the model that I'm relating as main #192

Closed rlzdesenv closed 1 year ago

rlzdesenv commented 1 year ago

I need to bring only the data from the "documents", but it is bringing all the data from the relationships involved, I also need to bring the "document" using group by id of the "document"

that way it brings me all data from all related tables

return $fichaTecnica->load([            
            'proprietarios:id,nome,documento',
            'alvaras:*'
]);

this way only from the documents table, however it gets weird I can't load the related models for example I needed to bring the user who created the document

return $fichaTecnica->load([            
            'proprietarios:id,nome,documento',
            'alvaras:documentos.*'
]);

image

relacionamento

<?php

class FichaTecnica extends Model
{
    protected $table = 'safira.ficha_tecnicas';

    public function alvaras(): HasManyThrough
    {
        return $this->hasManyDeep(Documento::class, [Area::class, AreaDocumento::class]);
    }

}
<?php

class Area extends Model
{
    protected $table = 'safira.areas';
    use SoftDeletes;
}
<?php

class AreaDocumento extends Pivot
{
    use SoftDeletes;

    protected $table = 'safira.area_documento';
    protected $fillable = [
        'area_id',
        'documento_id',
    ];
}
<?php

class Documento extends Model
{
    use SoftDeletes;

    protected $table = 'geral.documentos';

    public function usuario(): BelongsTo
    {
        return $this->belongsTo(Usuario::class);
    }

}
elvispdosreis commented 1 year ago

I have the same difficulty

staudenmeir commented 1 year ago

Hi @rlzdesenv, Please log the executed queries with the Laravel query log or a tool like debugbar.

elvispdosreis commented 1 year ago

this is the related select

select
  distinct "documentos".*,
  "safira"."areas"."ficha_tecnica_id" as "laravel_through_key"
from
  "geral"."documentos"
  inner join "safira"."area_documento" on "safira"."area_documento"."documento_id" = "geral"."documentos"."id"
  inner join "safira"."areas" on "safira"."areas"."id" = "safira"."area_documento"."area_id"
where
  "area_documento"."tipo" = 'alvara'
  and "safira"."areas"."ficha_tecnica_id" in (2)
  and "geral"."documentos"."deleted_at" is null
  and "safira"."area_documento"."deleted_at" is null
  and "safira"."areas"."deleted_at" is null
return $fichaTecnica->load([
            'areas:*',
            'areas.uso:id,nome',
            'areas.tipo:id,nome',
            'areas.estrutura:id,nome,sisobra',
            'areas.destinacao:id,nome,sisobra',
            'areas.operacao:id,nome,sisobra',
            'areas.tipoRegularizacao:id,nome',
            'responsaveis:*',
            'responsaveis.formacao:id,nome,tipo,conselho_id',
            'responsaveis.formacao.conselho:id,nome,sigla',
            'responsaveis.contribuinte:id,nome,documento',
            'proprietarios:id,nome,documento',
            'alvaras:documentos.*'
        ]);

the load was a little out of the standard to declare in the load 'alvaras:documentos.*', plus it brings the data of the documento, however I cannot relate in the loading of the documento the user of the table documento 'alvaras:documentos.user:id,name'

the normal declaration should be 'alvara.documentos:*' 'alvara.documentos.usuario:id,nome', but it doesn't work

staudenmeir commented 1 year ago

I need to bring only the data from the "documents", but it is bringing all the data from the relationships involved that way it brings me all data from all related tables

What do you mean by "all the data from the relationships"? What query is executed in this case?

'alvara.documentos.usuario:id,nome', but it doesn't work

Is there an error or is the query result just wrong?

rlzdesenv commented 1 year ago

if I declare 'alvaras.documentos.usuario:id,nome' like this I get this error

Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [documentos] on model [App\Models\Documento].

rlzdesenv commented 1 year ago

I apologize, I think I was wrongly relating the load.

I was using 'alvara.documentos.usuario:id,nome' but I preferred to use only 'alvaras.usuario:id'

because the relationship below is directly linked to the module I want

<?php

class FichaTecnica extends Model
{
    protected $table = 'safira.ficha_tecnicas';

    public function alvaras(): HasManyThrough
    {
        return $this->hasManyDeep(Documento::class, [Area::class, AreaDocumento::class]);
    }
}
staudenmeir commented 1 year ago

I was using 'alvara.documentos.usuario:id,nome' but I preferred to use only 'alvaras.usuario:id'

So alvara.documentos.usuario:id,nome works but alvaras.usuario:id doesn't work, right? What error are you getting in this case?