Level 58
Why not try using a map() function to make it more efficient? Something like this:
public function findProducts(Request $request)
{
$product = [];
if ($request->has('q')) {
$search = $request->q;
$pro = Product::with('productDetails')
->select("id", "title")
->where('title', 'LIKE', "%$search%")
->get();
$product = $pro->map(function ($item) {
return $item->productDetails->map(function ($detail) use ($item) {
return [
'id' => $detail->id,
'text' => $detail->type . ' ' . $item->title . ' ' . $detail->weight . ' ' . $detail->weightUnitOptions()[$detail->weight_unit] . ' | ' . $detail->sale_price
];
});
});
}
return response()->json($product);
}
Now you can get your products and details in a much more efficient way!