Today, we’ll take the text-based suggestions from GPT (like “Gaming Chair” or “Monitor Arm”) and match them against real products in your database to return full product details.
🧩 Step 1: Update ProductRecommendationService
to match suggestions
In recommend()
method, replace the final return logic with:
use App\Models\Product;
$matchedProducts = Product::query()
->where(function ($query) use ($decoded) {
foreach ($decoded['suggestions'] ?? [] as $suggestion) {
$query->orWhere('name', 'LIKE', '%' . $suggestion . '%');
}
})
->limit(5)
->get();
return $matchedProducts;
Your updated recommend()
method now returns a Collection of Product models.
🧠 Optional: Add fallback matching (description/category)
You can enhance it:
->orWhere('description', 'LIKE', '%' . $suggestion . '%')
->orWhere('category', 'LIKE', '%' . $suggestion . '%')
🧪 Step 2: Test route with JSON product output
In web.php
:
Route::get('/recommendations', function (ProductRecommendationService $service) {
$user = \App\Models\User::first(); // demo
$products = $service->recommend($user);
return response()->json($products);
});
Expected output:
[
{
"id": 7,
"name": "Gaming Chair",
"description": "Comfortable chair for long gaming sessions",
"price": "450.00",
"category": "Furniture"
},
...
]
🎯 Bonus: Add a score system (optional)
To return better matches, you can use Laravel Scout or similarity algorithms like Levenshtein distance or full-text search in future days.
✅ Summary
✅ Today you:
- Took GPT output (plain text)
- Matched it to real products in the DB
- Returned full product info ready for UI display
✅ Up next (Day 5): We’ll build a Blade view to display recommendations in a card-based layout, complete with images, titles, and “View More” links.