2017-07-06 44 views
0

我有这个疑问Laravel,如何选择数据,如果关系条件为真

$organisationUserNotifications = OrganisationUserNotification:: 
      whereHas('user',function($user){ 
       $user->where('status',STATUS_ACTIVE); 
      }) 
      ->orWhere('new_comment',ORGANISATION_NOTIFICATION_ACTIVE) 
      ->orWhere('new_review',ORGANISATION_NOTIFICATION_ACTIVE) 
      ->orWhere('new_salary',ORGANISATION_NOTIFICATION_ACTIVE) 
      ->get(); 

这个查询得到结果谁拥有new_comment->1new_review->1new_salary->1

所有的用户,我有user

idnamestatus
1 | us_1 | 0
2 | us_2 | 1
3 | us_3 | 1

notification

idnew_commentnew_reviewnew_salary
1 | 0 | 1 | 1
2 | 1 | 0 | 1
3 | 1 | 0 | 1

此查询获取所有行,因为用户1具有status 0或请任何想法 如何验证等where如果用户是活跃的,没有leftjoin

回答

0

好吧,我发现如果解任何人都需要它

$organisationUserNotifications = OrganisationUserNotification:: 
     whereHas('user',function($user){ 
      $user->where('status',STATUS_ACTIVE); 
     }) 
     ->where(function($query){ 
      $query->orWhere('new_comment',ORGANISATION_NOTIFICATION_ACTIVE); 
      $query->orWhere('new_review',ORGANISATION_NOTIFICATION_ACTIVE); 
      $query->orWhere('new_salary',ORGANISATION_NOTIFICATION_ACTIVE); 
     }) 
     ->get();