2016-07-26 2 views
2

PostsHistory 모델에 행이없는 모든 Post (Model : Post)를 반환하려고합니다.Laravel relationship`whereNotIn` 다른 테이블

내 코드는 다음과 같습니다

public function posts(){ 
    return $this->hasMany('App\Post'); 
} 

public function remaining_posts(){ 
    $history = PostHistory::all()->pluck('id'); 
    return $this->posts->whereNotIn('post_id', $history); 
} 

하지만 오류

[BadMethodCallException]  
    Method whereNotIn does not exist. 

내가 관계를 통해 remaining_posts를 얻을 수 또는 전용 컨트롤러에서 수행 할 수있는 방법이 있나요 얻을?

답변

1

편집 마지막 라인 :

return $this->posts()->whereNotIn('post_id', $history)->get(); 

당신은 posts()posts을 변경해야
  1. . postswhereNotIn() 메서드가없는 Illuminate\Database\Eloquent\Collection 개체입니다. 여기서 posts()whereNotIn() 메서드가 정의 된 Illuminate\Database\Eloquent\Relations\HasMany 개체를 반환합니다.

  2. whereNotIn()Illuminate\Database\Query\Builder 개체를 반환합니다. 따라서 컬렉션을 받으려면 get()으로 전화해야합니다. 그러나 flexibilty가 remaining_posts() 메서드에 다른 Builder 메서드를 연결하려면 get()을 사용하지 마십시오.

+0

설명해 주셔서 감사합니다 :) – George