Larastan: Custom scopes in with/withCount closures

Created on 22 Oct 2018  路  4Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.3.4
  • --level used: 5 (default)

Description:

 ------ ------------------------------------------------------------------------------------- 
  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.

Laravel code where the issue was found:

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();
false positive

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Braunson picture Braunson  路  4Comments

JeroenVanOort picture JeroenVanOort  路  3Comments

Gummibeer picture Gummibeer  路  3Comments

fico7489 picture fico7489  路  3Comments

jdrieghe picture jdrieghe  路  4Comments