plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: Model-specific properties/attributes/methods won't get recognized using ::query()

Open xHeaven opened this issue 1 year ago • 4 comments

Bug description

Model-specific properties/attributes/methods/relations won't get recognized anymore if you are using Model::query().

For example, this works - tasks is autocompleted:

$tasks = User::first()->tasks;

This doesn't work - tasks won't get autocompleted:

$tasks = User::query()->first()->tasks;

Model "query helpers" aka whereSomeField do get autocompleted with both methods.

Plugin version

8.2.3.241

Operating system

Linux

Steps to reproduce

  1. Create a model
  2. Add any property/attribute/relation/method/relation
  3. Try to make it get autocompleted "normally" as mentioned in the bug description - it'll get autocompleted
  4. Now try to get it autocompleted the "::query()" way as described above - it won't get autocompleted

Relevant log output

No response

xHeaven avatar Aug 07 '24 23:08 xHeaven

I have reported this issue once before (https://github.com/laravel-idea/plugin/issues/933), but @adelf said that he doesn't know how to fix it. I wanted to re-open this issue and ask for it to be re-looked at, as clearly, it's not only me who likes to start queries with the query() method (https://x.com/ste_bau/status/1845536229925777916/photo/1, https://www.reddit.com/media?url=https%3A%2F%2Fpreview.redd.it%2Fghtykihdnek81.png%3Fwidth%3D1228%26format%3Dpng%26auto%3Dwebp%26s%3D20125620584fec14b2ac87edd4851c08ecd2aff4). Can query() method not be treated similarly to select()?

Thanks

azgooon avatar Oct 14 '24 08:10 azgooon

I have reported this issue once before (#933), but @adelf said that he doesn't know how to fix it. I wanted to re-open this issue and ask for it to be re-looked at, as clearly, it's not only me who likes to start queries with the query() method (x.com/ste_bau/status/1845536229925777916/photo/1, reddit.com/media?url=https%3A%2F%2Fpreview.redd.it%2Fghtykihdnek81.png%3Fwidth%3D1228%26format%3Dpng%26auto%3Dwebp%26s%3D20125620584fec14b2ac87edd4851c08ecd2aff4). Can query() method not be treated similarly to select()?

Thanks

Unfortunately, I don't think there is a way for us to contribute to the project, so as long as he can't figure it out, we are on our own. Or is there a way @adelf?

xHeaven avatar Dec 19 '24 17:12 xHeaven

It's really hard to reproduce. I tried many times. It works in my PhpStorm. I understand it sounds stereotypical...

adelf avatar Dec 19 '24 18:12 adelf

Funnily enough, I can also see a few examples that do work and some that doesn't in the same project, with seemlingly the same exact structure.

class Company extends Model
{
    // ...

    /**
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    // ...
}

class Application extends Model
{
    // ...

    /**
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    // ...
}

Company::query()->with('user')->get(); // This does NOT work
Application::query()->with('user')->get(); // This does work

Makes absolutely no sense to me why this is happening.

xHeaven avatar Dec 20 '24 03:12 xHeaven