Laravel Consultation

2

Hi, I have a query in Laravel 5.5 that brings me what I need with the related data of 2 tables. My query is as follows:

$facturas = ProgramacionPagos::select('*')
            ->where([
                ['pagorealizado' , '0'], ['autorizado_cxp', '!=' , NULL]
            ])
            ->with('files')
            ->with('comments')
            ->get();

and if you bring what I need from the following address:

What I want is to be able to access the values of the related tables. I've tried to do it with

@foreach($facturas as $k => $factura) 
$factura->files->id

but I get an error. Someone who can help me

    
asked by Carlos Agaton 10.01.2018 в 19:20
source

2 answers

2

You could use the each collections by Laravel, (which I think would be the best option)

$facturas->files->each(function($archivo, $indice){
    // Realizas lo pertinente con cada objeto $archivo
    echo $archivo->id;
});

Or you could transform the whole object to an original array of PHP and do the following:

$facturas = ProgramacionPagos::select('*')
            ->where([
                ['pagorealizado' , '0'], ['autorizado_cxp', '!=' , NULL]
            ])
            ->with('files')
            ->with('comments')
            ->get()
            ->toArray(); // Convertimos nuestra colección a un arreglo simple

// Accedemos al arreglo 'files' dentro de nuestro arreglo facturas
foreach($facturas['files'] as $indice => $archivo){
    // Realizas lo pertinente con cada objeto $archivo
    echo $archivo['id'];
}
    
answered by 10.01.2018 / 20:42
source
0

To take a tour you should walk it first to an array with toArray() , something like this:

$facturas = ProgramacionPagos::select('*')
  ->where([
    ['pagorealizado' , '0'], ['autorizado_cxp', '!=' , NULL]
  ])
  ->with('files')
  ->with('comments')
  ->get()
  ->toArray();
    
answered by 10.01.2018 в 20:16