0

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;
    }
}

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, It is working exactly how should be. Obviously I've put it in a wrong model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.