Call Model Scope from Another Model Scope

Submitted by Mahmoud_Zalt - 9 years ago

in this example there is a one to many relation. [course has many chapters]

1. in the Course model
----------------------

public function scopeWithChapters($query)
{
    return $query->with(['chapters' => function($q)
    {
        $q->published(); // < this scope function is in the chapter model
    }]);
}

public function chapters()
{
    return $this->hasMany('Moubarmij\Models\Chapter’); // < the relation
}






2. in the Chapter model
-----------------------

public function scopePublished($query)
{
    return $query->where('published', '=', '1');
}