问题
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:
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');
来源:https://stackoverflow.com/questions/26777361/laravel-4-multiple-where-with-or-in-eloquent