Laravel 4: multiple where with or in eloquent

[亡魂溺海] 提交于 2019-12-02 15:31:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!