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

QueriesRelationships::has typehint is incorrect when $relation is a string #54581

Closed
rikvdh opened this issue Feb 12, 2025 · 2 comments
Closed

Comments

@rikvdh
Copy link
Contributor

rikvdh commented Feb 12, 2025

Laravel Version

11.42.0

PHP Version

8.3.16

Database Driver & Version

n/a

Description

This PR: #54452
Fixes the typehint of the callback in the has function. But when $relation is passed as a string the 'TRelatedModel' is left 'anonymous' and the typehint fails to resolve to the related model.

When then using e.g. scopes in the closure, static analysis says (larastan in this case) the type is wrong.
I'm also doubting if this is something Larastan 'should fix'.

Steps To Reproduce

So to stay with the examples from the PR:

This works:

$query->whereHas($user->posts(), function ($query) {
    // Knows that it's a Post Builder!
    assertType('Illuminate\Database\Eloquent\Builder<Illuminate\Types\Builder\Post>', $query);
});

But this doesnt:

$query->whereHas('posts', function ($query) {
    // Doesnt know what builder it is when first argument is a string, only that its a Model of some kind.
    assertType('Builder<Model>', $query);
});
@rikvdh
Copy link
Contributor Author

rikvdh commented Feb 12, 2025

I see my issue is similar to #54573 (although the title is a bit indescriptive).

@rikvdh rikvdh closed this as completed Feb 12, 2025
@calebdw
Copy link
Contributor

calebdw commented Feb 12, 2025

String relationships are handled in larastan/larastan#2048 and https://github.com/calebdw/larastan

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

No branches or pull requests

2 participants