VScode often has trouble understanding Laravel code correctly.
Getting a yellow squiggly line and an error but my query works as expected
I have this controller method :
public function showByCategory(Request $request, $categorySlug)
{
$category = Category::where('name', $categorySlug)->firstOrFail();
$listings = Listing::whereHas('item', function ($query) use ($category) {
$query->whereHas('sub_category', function ($subQuery) use ($category) {
$subQuery->where('category_id', $category->id);
});
})
->with('item.sub_category')
->orderBy('updated_at', 'desc')
->paginate(20);
if ($listings->currentPage() > $listings->lastPage()) {
abort(404);
}
return Inertia::render('CategoryListings', [
'listings' => $listings,
'currentCategory' => $category->bg_name,
]);
}
and on this line $listings = Listing::whereHas('item', function ($query) use ($category) {
I'm getting a yellow squiggly underline on the 'item' parameter, my query is functioning as expected but VScode says :
Argument '1' passed to whereHas() is expected to be of type Illuminate\Database\Eloquent\Relations\Relation, string given
My relationships are defined as follows: Each Item has many listings and each listing belongs to an item. Each subcategory has many items and each item belongs to a subcategory. Each category has many subcategories and each subcategory belongs to a category.
So example would be : Category:Electronics,Subcategory:ComputerParts,Item:GPUs.
I'm brand new to Eloquent and chatgpt helped with writing that query, it is all working great, there is no n+1 problem and pagination works perfectly also. I just don't know if this error is something to be worried about and what it is exactly that I'm doing wrong.
Please or to participate in this conversation.
