Laravel 5.5 using Scope in model returns error for undefined method

When I try to use scope in this situation, returns me this error:

Call to undefined method Illuminate\Database\Query\Builder::isPromotionTypeIdScope() (View: C:\MAMP\htdocs\mysite\resources\views\site\home.blade.php)

Logic is:

If I replace isPromotionTypeIdScope() with all of the clauses (from the scope), works, but if I use scope gives me error, any suggestions?

Something about the structure is not working. I'm using scopes in another models and have no issues with them. Cannot find what's wrong.

is it possible to be, because I'm trying to add scope (Example: ->promotion()->isPromotionTypeIdScope($promotion_type_id))?

    public function product()
{
    return $this->belongsTo('App\Models\Product', 'product_id');
}

public function promotion(){
    return $this->belongsToMany('App\Models\Promotion', 'promotion_product_prices', 'product_price_id', 'promotion_id');
}



public function single_promotion($promotion_type_id = 0){ 

    return $this->promotion()->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;

}

public function category_promotion($promotion_type_id = 0){
    return $this->product()->first()
                            ->category()
                            ->first()
                            ->promotion()
                            ->isPromotionTypeIdScope($promotion_type_id)
                            ->first() ?? false;

}


public function full_promotion($promotion_type_id = 0)
{
      return Promotion::where('full', 1)->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;
}



public function hasPromotion($promotion_type_id = 0){
    if($this->full_promotion($promotion_type_id) !== false){
        return $this->full_promotion($promotion_type_id);
    }elseif($this->category_promotion($promotion_type_id) !== false){
        return $this->category_promotion($promotion_type_id);
    }elseif($this->single_promotion($promotion_type_id) !== false){
        return $this->single_promotion($promotion_type_id);
    }else{
        return false;
    }

}

public function scopeIsPromotionTypeIdScope($query, $promotion_type_id=0){

    if($promotion_type_id != 0){
        return $query->where('promotion_type_id', $promotion_type_id)
                                        ->where('validity_from', '<=', date('Y-m-d H:i:s'))
                                        ->where('validity_to', '>=', date('Y-m-d H:i:s'))
                                        ->where('active', 1)
                                        ->orderBy('updated_at', 'DESC')->limit(1);
    }else{
        return $query;
    }
}

Upvotes: 0

Views: 2061

Answers (1)

yrv16
yrv16

Reputation: 2275

Everywhere you call your isPromotionTypeIdScope method on Promotion model so you should define scopeIsPromotionTypeIdScope in Promotion model instead of this one.

Upvotes: 2

Related Questions