Laravel 4: multiple where with or in eloquent

2019-03-03 21:18发布

how to write this condition using laravel eloquent

SQL

select count(id) from fight where status = 'finished' and (user1 = 1 or user2 = 1)

Laravel (not finished):

Fight::where('user1','=',$uid)->orWhere('user2', '=', $uid)->count('id');

thanks,

1条回答
混吃等死
2楼-- · 2019-03-03 21:48

This should do the work:

Fight::whereStatus('finished')->where(function($q) use ($uid) {
    $q->where('user1',$uid)->orWhere('user2', $uid);
})->count('id');

EDIT

Answering comment:

Fight::whereIn('status', ['finished', 'cancelled'])->where(function($q) use ($uid) {
    $q->where('user1',$uid)->orWhere('user2', $uid);
})->count('id');
查看更多
登录 后发表回答