2017-01-12 3 views
1

피벗 테이블의 조건을 확인해야합니다. 나는 이것으로 하나의 상태를 확인할 수 있다는 것을 안다 :laravel 5.1 : 어떻게 웅변에 두 개의 피벗을 사용할 수 있습니까?

$dis = $user->discounts()->wherePivot('used_for_id', '=', null) 

그러나 나는 두 가지 조건을 원한다. orWherePivot을 사용할 때 조건이 OR 인 두 개가 함께 처리되었지만 함께 처리하려면 AND이 필요합니다.

$whereData = [ 
    ['id', "=", $discountId], 
    ['used_for_id', "=", null] 
]; 
+0

내 질문에 대한 추가 좋아 만 점 : 기본에 대한 ID가 어디에서 있기 때문에 가 wherePivot에서 "ID를"해결되지 . 나는 그것을 -> wherePivot ('discount_id', $ discountId) -> wherePivot ('used_for_type', null)로 변경하고 올바르게 작동합니다! –

답변

3

wherePivot()은 정상적인 where() 방법과 동일하게 작동합니다. 방금 제 wherePivot() 조건에 체인 수 있으며 이전의 조건 AND 혼성 될 것이다

$dis = $user 
    ->discounts() 
    ->wherePivot('id', '=', $discountId) 
    ->wherePivot('used_for_id', '=', null) 
    ->get(); 
2

wherePivot 함수의 정의는 다음과 주어진다. 그래서 u는 조건을 결합과 같은 '또는'$ 부울 변수 사용 '및'수

public function wherePivot($column, $operator = null, $value = null, $boolean = 'and') { 
    //code 
} 

$dis = $user->discounts()->wherePivot('column', '=', null,'and')  
     ->wherePivot('column', '=', null,'or')  
     ->wherePivot('column', '=', null,'and')  
     ->get(); 
+0

'orWherePivot()'은 마지막 매개 변수로 "or"를 사용하여'wherePivot()'을 호출하는 바로 가기 방법입니다. OP는 그들이 원하는 바가 아니라고 말했다. – patricus

관련 문제