--level used: 5 (default) ------ -------------------------------------------------------------------------------------
Line app/Http/Controllers/Browse/CatalogueController.php
------ -------------------------------------------------------------------------------------
95 Call to an undefined method Illuminate\Database\Eloquent\Builder::matchingSearch().
------ -------------------------------------------------------------------------------------
The issue here is that my Product model has public function scopeMatchingSearch(Builder $query) {...}
however larastan is not picking up that context inside the closure, meaning it believes I only have access to the query builder methods and not the model scopes.
class Product extends Model {
public function scopeMatchingSearch(Builder $query, $term): Builder
{
return $query->where('title', 'like', "%{$term}%")
->orWhere('code', 'like', "%{$term}%")
}
}
class Catalogue extends Model {
public function products(): HasMany
{
return $this->hasMany(Product::class, 'catalogue_id');
}
}
Catalogue::withCount(['products' => function (Builder $query) use ($request) {
$query->matchingSearch($request->input('search')); //False positive
}])->get();
I noticed a similar issue, I have a query which has 3 scopes chained, 1 scope works and is analyzed correctly, the second I add any more scopes it complains that that scope does not exist.
I have a similar issue. When i call a HasMany relationship method on a model instance, and then chain a scope onto that, it complains that Illuminate\Database\Eloquent\Relations\HasMany does not have that method.
Can you re-test this with 0.4.3?
While the problem described in this issue should have been fixed, https://github.com/nunomaduro/larastan/issues/351 came up instead
Most helpful comment
I have a similar issue. When i call a HasMany relationship method on a model instance, and then chain a scope onto that, it complains that Illuminate\Database\Eloquent\Relations\HasMany does not have that method.