How to tour a Collection in Laravel

2

This time I have a mental gap and I do not know what is escaping me ....

I have the following collection, annex image:

I have a model called Income and another called DetailInside, in which in the relations I have Income to Detail Income and vice versa "An income have many details of income and many details of income belong to an income" in my model the relationship is so:

from income to income detail the relationship is called: detalleIngreso

detail of income to income the relationship is called: income

Well everything is fine there, the issue is that I'm doing an eloquent query to income and I do it like this:

$verDatosIngreso=Ingreso::orderBy('id')
        ->with('detalleIngreso')
        ->where('id','=',$id)
        ->get();

it is assumed afterwards that in order to access "service_id" of the detail-relationship relationship, I must go through the array

foreach($verDatosIngreso as $c)
{
  $c->detalleIngreso->servicio_id
}

But I'm not working, I know I'm missing something but I do not remember what the hell it is.

I hope you can clarify the hazing that I am committing the truth I do not remember.

Thank you ...

    
asked by jose angarita 15.07.2018 в 00:04
source

3 answers

1

What happens is that the relationship between Ingreso and DetalleIngreso is one to many:

Ingreso 1 ---- m DetalleIngreso

Then, when accessing the relationship through Ingreso , the result is a collection of objects.

If you have a collection of Ingreso models and you want to access the objects belonging to the detalleIngreso relationship, it means that you must access the objects in a collection ( Ingreso ) of collections ( DetalleIngreso ) .

You can not directly access the attributes of a collection without traversing it. For this query is wrong:

$verDatosIngreso = Ingreso::orderBy('id')
    ->with('detalleIngreso')
    ->where('id','=',$id)
    ->get();

foreach($verDatosIngreso as $c)
{
  $c->detalleIngreso->servicio_id //  $c->detalleIngreso es una colección
}

The solution would be to access the arrangement by going through the collection of the relationship:

foreach($verDatosIngreso as $ingreso)
{
    foreach($ingreso->detalleIngreso as $detalle)
    {
      $detalle->servicio_id
      // alguna otra acción
    }
}

On the other hand, since we are working with collections, you could use the each() function that is specific to collections:

$verDatosIngreso = Ingreso::orderBy('id')
        ->with('detalleIngreso')
        ->where('id', '=', $id)
        ->get()
        ->each(function ($ingreso) {
            $ingreso->detalleIngreso->each(function ($detalle) {
                $detalle->servicio_id;
                // alguna otra acción
            });
        });
    
answered by 15.07.2018 в 07:38
0

Try this

@foreach($verDatosIngreso as $c)  
    {{$c['detalleIngreso']->servicio_id}}  
@endforeach

I'm sure you enter here to enter detail

@foreach($verDatosIngreso as $c)  
    {{$c['detalleIngreso']}}  
@endforeach

But as it is a collection within another collection, it would be to continue testing

    
answered by 15.07.2018 в 03:07
0

You can use the each method of the Collections and then re-iterate the relationship, more or less as follows:

$verDatosIngreso->each(function ($datoIngreso, $indice) {
    $datoIngreso->detalleIngreso->each(function ($detale, $subIndice) {
        $detalle->servicio_id;
    });
});
    
answered by 15.07.2018 в 03:17