Merhaba arkadaşlar,
Başlığa ne yazmam gerektiğine tam emin olamadım ama durumu açıklamaya çalışayım.
Şöyle bir api rotam var (routes/api.php)
Route::get('productdetail/{category_slug}/{subcategory_slug}/{product_slug}','Api\ProductDetailController');
Bu gelen slug değerlerine göre sorgu yapıp ürünü ilişkili tablolarıyla çekmek istiyorum. Aynı zamanda gelen değer doğru değilse 404 response vermesi için firstOrFail() kullanmak istedim. Controller tarafında şu şekilde hazırladım ancak POSTMAN'dan istek gönderdiğimde "No query results for model [App\\Models\\Product]."
hatası alıyorum.
Api/ProductDetailController.php;
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Models\SubCategory;
class ProductDetailController extends Controller
{
/**
* Handle the incoming request.
*
* @return \Illuminate\Http\Response
*/
public function __invoke(Category $c_slug, SubCategory $sc_slug, Product $p_slug)
{
$productDetail = Product::with(['category' => function ($query) use ($c_slug) {
$query->where('slug', $c_slug)->firstOrFail();
}, 'subCategory' => function ($query) use ($sc_slug) {
$query->where('slug', $sc_slug)->firstOrFail();
},'images','price','seller'])->where('slug', $p_slug)->firstOrFail();
return $productDetail;
}
}