在 Laravel 中默认提供了 orWhere 方法,但是如果直接与 Where 方法连接查询,得到的结果是不正确的,Laravel 中实现 where or 查询要按照下面的方法:

Model::where(function ($query) {
    $query->where($where1)
          ->orWhere($where2);
})->where($where)->first();

示例1:查询年龄大于18或者性别为女的学生

$students = Student::where(function ($query) {
    $query->where('age','>',18)
          ->orWhere('sex','=','女');
})->get();

示例2:查询邮箱或者用户名等于输入值的用户

$username = Request::input('username')
$user = User::where(function ($query) user ($username) {
    $query->where('username','=',$username)
          ->orWhere('email','=',$username);
})->first();