Get only 1 Item from a collection in laravel?

0

I hope you can help me.

I have this query in laravel

$poa = DB::table('poa_formatos')
                ->join('entities', 'poa_formatos.entity_id', '=', 'entities.id')
                ->join('acciones_especificas', 'acciones_especificas.poa_formatos_id', '=', 'poa_formatos.id')
                ->join('acciones_poa', 'acciones_poa.accion_especifica_id', '=', 'acciones_especificas.id')
                ->join('objetivos_nacionales', 'poa_formatos.obj_nacional_id', '=', 'objetivos_nacionales.id')
                ->select('entities.name as nombre_ente', 'acciones_especificas.*', 'acciones_especificas.id as id_accion', 'acciones_especificas.nombre as accion_nombre', 'objetivos_nacionales.item', 'objetivos_nacionales.redaccion as obj_nacional', 'poa_formatos.*')
                ->where('poa_formatos.entity_id', '=', $entity->id)
                ->get();

    dd($poa);

With the dd he returns the next collection

How do I get only the id_accion? I want to make a dd that only brings me this Item because I need it for another query. I mean something like $ poa-> id_accion!

I do not know if I explain myself, I hope you can help me, thanks in advance.

    
asked by Carlos Joker 03.12.2018 в 17:49
source

3 answers

0

In your select you can do something like this:

->select('acciones_especificas.id as id_accion')
    
answered by 05.12.2018 в 19:34
0

add the following code at the end of your query, try replacing it with the -> get ()

->first()
    
answered by 05.12.2018 в 21:30
0

If I have not misunderstood you, you want all the id_accion that takes out that query but only those fields, no? In that case it would be like this:

$poa = DB::table('poa_formatos')
                ->join('entities', 'poa_formatos.entity_id', '=', 'entities.id')
                ->join('acciones_especificas', 'acciones_especificas.poa_formatos_id', '=', 'poa_formatos.id')
                ->join('acciones_poa', 'acciones_poa.accion_especifica_id', '=', 'acciones_especificas.id')
                ->join('objetivos_nacionales', 'poa_formatos.obj_nacional_id', '=', 'objetivos_nacionales.id')
                ->where('poa_formatos.entity_id', '=', $entity->id)
                ->get()->pluck('acciones_especificas.id');

If that is what you are looking for, you do not need to call the select method, since you only want to obtain that field.

    
answered by 05.12.2018 в 22:03