I try to add new method for simplify and reusable code but I failed
User Model :
public function products()
{
return $this->hasMany('App\Product');
}
public function obsolate()
{
return $this->where('status', 'obsolate');
}
When I try to retrieve like auth()->user()->products()->obsolate()
it gives some error like
BadMethodCallException: Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::obsolate()
But If I do like auth()->user()->products()->where('status', 'obsolate')->get()
It works.
Please, correct me if I wrong...
UPDATE
Product Model :
public function user()
{
return $this->belongsTo('App\User');
}
public function scopeObsolate($query)
{
return $query->where('status', 'obsolate');
}
I do auth()->user()->products->obsolate()->get()
and it works!
But, if I want to use constraint group to solve this problem it will return error
public function scopeObsolate(Builder $builder)
{
return $builder->where(function (Builder $query) {
return $query->where('status', 'obsolate');
});
}
Argument 1 passed to App\Product::App{closure}() must be an instance of App\Builder, instance of Illuminate\Database\Eloquent\Builder given
SOLVED
use Illuminate\Database\Eloquent\Builder