Skip to content

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

Closed
@rikvdh

Description

@rikvdh

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);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions