laravel-ide-helper
laravel-ide-helper copied to clipboard
IDE doesn't recognize model that fetched from a relation query
Versions:
- ide-helper Version: 2.10.0
- Laravel Version: 8.50.0
- PHP Version: 8.0.8
Summary:
The IDE doesn't recognize model that got fetched from a relation query.
class Book extend Model
{
public function authors(): BelongToMany
{
return $this->belongsToMany(Author::class)->withTimestamps();
}
}
$book = Book::first();
// PHPStorm recognizes `$author` as `Author|mixed` - which is good
$author = $book->authors[0];
// PHPStorm recognizes `$author2` as `Model|BelongsToMany|null`
$author2 = $book->authors()->firstOrFail();
// PHPStorm recognizes `$author3` as `mixed`
$author3 = $book->authors()->get()->first();
// PHPStorm recognizes `$author4` as `mixed`
$author4 = $book->authors->first();
As you can see, the IDE didn't recognize correctly the type of $author2 & $author3 & $author4.