How to send the result of two queries by AJAX in laravel

1

Good evening I'm using laravel 5.6 and I want to make a data request by ajax using an id and with this I get the result of two queries. My questions is how I can respond to the ajax request with the result of the consultations.

These are my questions that are in the controller:

 $compras =DB::table('compras')
            ->join('proveedors', 'compras.id_proveedor', '=', 'proveedors.id_proveedor')
            ->select('id_compra','proveedors.nombre AS Empresa','fecha_compra','descripcion','estado')
            ->where('id_compra', '=', $id)
            ->get();
    $inventario=DB::table('inventarios')
            ->join('tipo_materials', 'inventarios.id_tipo_material', '=', 'tipo_materials.id_tipo_material')
            ->join('almacens', 'inventarios.id_almacen', '=', 'almacens.id_almacen')
            ->select('id_item','tipo_materials.descripcion AS Material','rango_inicial','rango_final','serie','cantidad','precio_unitario','observaciones','almacens.descripcion AS Almacen')
            ->where('id_compra', '=', $id)
            ->get();

How could this answer return?

 return response()->json(
                 ??????????    
                 );
    
asked by ALVARO ROBERTO BACARREZA ARZAB 10.05.2018 в 00:56
source

1 answer

1

To be able to do what you want I commented that, the JSON method accepts as an argument an associative array so that you can do the following

return response()->json(['compras' => $compras, 'inventario' => $inventario]);
  

Where the value between quotes is the key and the variable is the value   assigned

    
answered by 10.05.2018 / 01:07
source