두 가지 관련 모델이 있습니다 : Category및 Post.
Post모델이 갖는 published범위 (방법 scopePublished()).
해당 범위의 모든 범주를 얻으려고 할 때 :
$categories = Category::with('posts')->published()->get();오류가 발생합니다.
정의되지 않은 메서드 호출
published()
범주:
class Category extends \Eloquent
{
    public function posts()
    {
        return $this->HasMany('Post');
    }
}게시하다:
class Post extends \Eloquent
{
   public function category()
   {
       return $this->belongsTo('Category');
   }
   public function scopePublished($query)
   {
       return $query->where('published', 1);
   }
}답변
인라인으로 할 수 있습니다.
$categories = Category::with(['posts' => function ($q) {
  $q->published();
}])->get();관계를 정의 할 수도 있습니다.
public function postsPublished()
{
   return $this->hasMany('Post')->published();
   // or this way:
   // return $this->posts()->published();
}그리고:
//all posts
$category->posts;
// published only
$category->postsPublished;
// eager loading
$categories->with('postsPublished')->get();