0

I have product Model which belongs to Shop Model. Shop Can be Active or in Active, So I need to get products from active shops only. So I added this method

public function isActiveShop(){
    return $this->where($this->shop->status,'active');
}

and from my controller

$prodcuts = Product::where('id',$rawArray)->isActiveShop()->get();

And I get this error

"Call to undefined method Illuminate\Database\Query\Builder::isActiveShop()"

2 Answers 2

1

You have to use a scope and whereHas():

public function scopeIsActiveShop($query) {
    return $query->whereHas('shop', function($query) {
        $query->where('status', 'active');
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

That's work as I suppose but I got a new error Trying to get property of non-object
$this->shop->status looks a bit strange. Do you just mean ->where('status', 'active')?
shop is a relation method public function shop() { return $this->belongsTo(Shop::class); }
Thanks, that's works perfectly. any reference to study that part!!
0

Try this:

$products = (new Product())->isActiveShop()->where('id',$rawArray)->get();

1 Comment

That's work ,scope is better for solution as I think

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.