laravel-ide-helper
laravel-ide-helper copied to clipboard
Relationships dedicated query builder support
A month ago this package began to support dedicated builders https://github.com/barryvdh/laravel-ide-helper/pull/1089
// PostBuilder
class PostBuilder extends Builder
{
public function published(): static
{
$this->whereNotNull('published_at');
return $this;
}
}
// App\Models\Post.php
public function newEloquentBuilder($query)
{
return new PostBuilder($query);
}
// This is supported now because of PR #1089
Post::published()->get();
It will be extremely powerful if the dedicated builders support will be applied to relationships as well.
$user = User::first();
$userPosts = $user->posts()->published()->get();
PHPStorm and static analysis tools know that $user->posts()
returns a HasMany
instance, and because of this they throw an error for this piece of code:
Method 'published' not found in \Illuminate\Database\Eloquent\Relations\HasMany
The solution is to somehow add a @mixin PostBuilder
to this specific HasMany
instance.
@KaloyanYosifov Any idea how to implement it?
I guess this one is going to be a tough one to crack.
I guess we can generate a class in PostsHasMany
that extends HasMany
and add it as a return type in the doc block of the relationship method. But if you have a type hint on the method already it will break. That is just what I am assuming on a first glance.
Will check out it out on the weekend, if no one else does it first.
Wish you a great week @MatanYadaev 🙌
@barryvdh Do you think this feature is feasible?
Can you tell PHPStorm that $user->posts()
is HasMany
that extends PostBuilder
?