I'm trying to increase the speed of my query, currently it takes around 15 seconds for all the data to get returned. I have an Ionic 3 app which is sending a post request to get all inventory and my Laravel 5.4 server is handling the request.
Here's my query :
$input = file_get_contents( "php://input" );
$request = json_decode( $input );
$dealer_id = $request->dealer_id;
$tmp = Inventory::where(
'dealer_id', '=', $dealer_id
)->where(
'inventories.is_sold', '=', 0
)->where(
'is_active','=', 1
);
// dd($tmp);
$data = collect();
$pmt = $tmp->get();
logger( sprintf('# of rows returned: %s', $pmt->count() ) );
$pmt->each( function($row) use(&$data) {
logger( sprintf('Row : %s', $row->toJson() ));
$data->push( array(
'stock_number' => $row->stock_number,
'vehicle_id' => $row->vehicle_id,
'year' => $row->vehicle()->first()->year,
'make' => $row->vehicle()->first()->make,
'model' => $row->vehicle()->first()->model,
// 'trim' => $row->vehicle()->first()->trim,
'vin' => $row->vehicle()->first()->vin,
'status' => $row->vehicle_status,
'purchase_price' => $row->purchase_price,
'cost' => $row->cost,
// 'retail_price' => $row->retail_price,
'search_meta' => $row->search_meta,
// 'interior_color' => $row->vehicle()->first()->interior_color,
// 'exterior_color' => $row->vehicle()->first()->exterior_color,
'firstImg' => $row->getFirstImage(),
'images' => Vimage::select('vehicle_id','name'
)->where(
'dealer_id', '=', $row->dealer_id
)->where(
'vehicle_id', '=', $row->vehicle_id
)->get()
));
});
$statusKey = \App\lt_vehicle_status::where(
'dealer_id', '=', $dealer_id
)->where(
'is_active','=', 1
)->get();
$response = [
"status" => "Success",
"code" => "MAC01",
"reason" => "MAC - Inventory Gathered Successfully",
"data" => $data,
"status_keys" => $statusKey
];
echo json_encode( $response );
Data Returned:
https://i.imgur.com/zxWSNo5.png
One of the biggest issues is getting all the image urls as well as all the vehicles.
Thank you to whomever can help me try to increase my speed and efficiency of this.