Сначала я получаю свойства из таблицы 'properties', затем запускаю внутренний запрос, чтобы получить изображения из отдельной таблицы 'property_images'.Это медленный подход.Подскажите, как я могу получить свойства и изображения в одном запросе в моем запросе API?
public function searchProperty(Request $request){
if(count($request->all()) > 0){
$properties = array();
$properties = \DB::table('properties')
->where($request->all())
->get()->toArray();
$prop_ids = array();
if(isset($properties) && !empty($properties) && count($properties) > 0){
foreach($properties as $prop){
array_push($prop_ids, $prop->id);
}
$prop_images = \DB::table('property_images')
->whereIn('property_id', $prop_ids)
->get()->toArray();
if(isset($prop_images) && !empty($prop_images)){
foreach($prop_images as $imgs){
$upload_path = storage_path() . '/uploads/property/' . $imgs->property_id .'/'.$imgs->property_image;
array_push($properties, $upload_path);
}
}
echo '<pre>'; print_r($properties); exit;
return response()->json([
'success' => true,
'data' => $properties->toArray()
], 400);
}
}
else{
return response()->json([
'success' => false,
'message' => 'No data available'
], 500);
}
}