Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for scopes on relations #366

Open
stayallive opened this issue Aug 17, 2021 · 3 comments
Open

Support for scopes on relations #366

stayallive opened this issue Aug 17, 2021 · 3 comments

Comments

@stayallive
Copy link

Hey there, I'm not a 100% sure this is fixable by a plugin but it would be extremely nice if it somehow could...

Considering this code:

class Comment extends Model
{
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class)->withTrashed();
    }
}

The User model has the SoftDeletes trait but PhpStorm generates this error:

Method 'withTrashed' not found in \Illuminate\Database\Eloquent\Relations\BelongsTo

Which is totally correct of course, however we all know this does work.

The nice thing is that with Larastan even PHPStan supports it (PR) and I thought that maybe there is a way to teach PhpStorm to handle this too so that autocompletion works correctly on relations.

If it's also possible to get this code working that would be even more cool:

Comment::query()->first()->user()->withTrashed()->first();

I know this example doesn't make much sense but the gist is that it knows that the user() returns a relation that links to the user model so it knows withTrashed (or any other scope for that matter) is resolved.

If I missed something and this is already supported I'm sorry but I tried with the generated Eloquent code (which is crazy cool) and that did not seem to "fix" it.

Thanks in advance <3

@adelf
Copy link
Member

adelf commented Aug 17, 2021

image

The second one already works. PhpStorm here completes every method and correctly calculates the type for $user variable.

I'll try to add typing support for belongsTo() and other relation methods.

@adelf
Copy link
Member

adelf commented Aug 17, 2021

Forgot to say - you only need to generate an Eloquent helper code - https://laravel-idea.com/docs/eloquent

@stayallive
Copy link
Author

Yes, you are absolutely right the second example does work already (pretty good actually), not sure what I missed there when I wrote this :)

The first would be really cool if it could be achieved too 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants