Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thetanaz's avatar

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.

0 likes
11 replies
puklipo's avatar

VScode often has trouble understanding Laravel code correctly.

martinbean's avatar

@thetanaz You might get improved intellisense by using ::query() on models to being building queries:

$category = Category::query()->where('name', $categorySlug)->firstOrFail();

$listings = Listing::query()
    ->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);

This will help intellisense as it will now look for methods like whereHas on a query builder instance rather than a Model instance (as Eloquent models just proxy calls for undefined methods to the underlying query builder instance).

thetanaz's avatar

@martinbean So in essence it is an intellisense error, not an actual problematic error. That is what I thought because even though I copy-pasted the code from chatGPT I read through it like 100 times and I compared to the docs and I didn't seem to be doing anything wrong. And btw even when I add Listing::query I still get the same yellow squiggly line and same error under 'item'. Just getting VSCode to kinda sorta work with laravel was a task, considering the amount of extensions I had to install, so I'm not shocked that I'm having some intellisense problems.

martinbean's avatar

@thetanaz If you click through to the whereHas definition (Cmd + Click on macOS, probably Ctrl + Click on Windows) what does the actual method definition look like?

Also, what version of Laravel are you using?

thetanaz's avatar

@martinbean

  public function whereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1)
    {
        return $this->has($relation, $operator, $count, 'and', $callback);
    }

I'm using Laravel 11.16.0

edit: LOL there's even a yellow squiggly line in the QueriesRelationships with the same error https://imgur.com/Pbo9cV4

thetanaz's avatar

@martinbean Whoops, my bad

 /**
     * Add a relationship count / exists condition to the query with where clauses.
     *
     * @param  \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string  $relation
     * @param  \Closure|null  $callback
     * @param  string  $operator
     * @param  int  $count
     * @return $this
     */
    public function whereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1)
    {
        return $this->has($relation, $operator, $count, 'and', $callback);
    }

Seems like it should accept a string given @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation

martinbean's avatar

@thetanaz I don’t really know why it’s complaining when the docblock says it accepts string.

Do you have the PHP Intelephense extension installed?

The only other thing I can think of is, I also have the barryvdh/laravel-ide-helper package installed as a dev dependency, which may be helping VS Code.

thetanaz's avatar

I installed the laravel-ide-helper package as well and it didn't fix anything. I have PHP all-in-one support, PHP Debug,PHP Extension Pack, PHP Intelephense, PHP All-in-One PHP support by DevSense. After disablign php all-in-on php support the squiggly line is gone, I don't know why it was causing problems.

Please or to participate in this conversation.