2014-07-22 1 views
2

Laravel 응용 프로그램에서 many-to-many 관계를 분리하려고합니다. 그러나, 나는 다음과 같은 오류가 계속 : - UserItem분리가 정의되지 않았습니다.

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

을 나는이 개 모델이있다. User 각각은 Item이 많을 수 있지만 맞춤 테이블 인 user_haves에 있어야합니다.

항목 :

public function usersHave() { 
    return $this->hasMany('User', 'id', 'user_haves'); 
} 

사용자 : 나는이 작업을 수행 할 수있는 방법

Sentry::getUser()->haves()->detach($item->id); 

:

public function haves() { 
    return $this->hasMany('Item', 'id', 'users_have'); 
} 

는 이제 다음을 수행하여 분리하는 것을 시도하고있다 ? 두 경우 모두

답변

2

당신은 예를 들어 belongsToMany 대신 hasMany를 사용해야합니다

public function usersHave() { 
    return $this->belongsToMany('User', 'user_have'); 
} 

public function haves() { 
    return $this->belongsToMany('Item', 'user_have'); 
} 

는 또한 user_have 피벗 테이블을 작성해야합니다. Check the manual.

관련 문제