Darama
Darama

Reputation: 3370

How to call scope in model?

I have the following method in model:

public function announcements()
    {
        return $this->categories()->with("announcements");
    }

And in the same model:

public function scopeActive($query)
    {
        return $query->where('votes', '>', 100);
    }

Hot to call this local scope in model for:

return $this->categories()->with("announcements")->active(); ?

Upvotes: 2

Views: 3335

Answers (1)

Rickyrick
Rickyrick

Reputation: 475

I assume the categories is an relation on this model and the announcements is a relation on the categories model.

Then you can try:

return $this->active()->categories->with("announcements")

$this->active() will return the active records of this model.

->categories will get the related categories

->with("announcements") will eager load the announcements of all the categories.

This will return an eloquent query builder instance.

Upvotes: 1

Related Questions