Php Laravel和,多个或雄辩的查询未按预期工作

Php Laravel和,多个或雄辩的查询未按预期工作,php,mysql,laravel,laravel-5.3,Php,Mysql,Laravel,Laravel 5.3,查询: Need::Where('student_id', '!=', $id)->Where($matchto)->orWhere($matchfrom)->orWhere($matchstandard)->orWhere($matchlives_in)->orWhere($matchhobbies)->orWhere($matchskills)->orWhere($matchspecialization_1)->orWhere($matchde

查询:

Need::Where('student_id', '!=', $id)->Where($matchto)->orWhere($matchfrom)->orWhere($matchstandard)->orWhere($matchlives_in)->orWhere($matchhobbies)->orWhere($matchskills)->orWhere($matchspecialization_1)->orWhere($matchdesignation_1)->get();
它生成如下的结果查询

"query" => "select * from `needs` where `student_id` != ? and (`to` = ?) or (`from` = ?) or (`standard` = ?) or (`lives_in` = ?) or (`hobbies` = ?) or (`skills` = ?) or (`specialization_1` = ?) or (`designation_1` = ?)"
我想要的是

"query" => "select * from `needs` where `student_id` != ? and **(**(`to` = ?) or (`from` = ?) or (`standard` = ?) or (`lives_in` = ?) or (`hobbies` = ?) or (`skills` = ?) or (`specialization_1` = ?) or (`designation_1` = ?)**)**"

学生号“!=”后面加一个括号?和。我如何才能做到这一点?

您需要满足以下条件:

Need::Where('student_id', '!=', $id)
     ->where(function ($query) use ($matchto, $matchfrom, $matchstandard, $matchlives_in, $matchhobbies, $matchskills, $matchspecialization_1, $matchdesignation_1) {
        return $query->where($matchto)->orWhere($matchfrom)->orWhere($matchstandard)->orWhere($matchlives_in)->orWhere($matchhobbies)->orWhere($matchskills)->orWhere($matchspecialization_1)->orWhere($matchdesignation_1);
    })->get();

您需要嵌套您的条件:

Need::Where('student_id', '!=', $id)
     ->where(function ($query) use ($matchto, $matchfrom, $matchstandard, $matchlives_in, $matchhobbies, $matchskills, $matchspecialization_1, $matchdesignation_1) {
        return $query->where($matchto)->orWhere($matchfrom)->orWhere($matchstandard)->orWhere($matchlives_in)->orWhere($matchhobbies)->orWhere($matchskills)->orWhere($matchspecialization_1)->orWhere($matchdesignation_1);
    })->get();

它显示未定义的变量$matchto和嵌套中的所有其他变量,如何在嵌套条件中传递这些变量使用($variable)需要::Where('student_id','!=',$id)->Where(function($query)use($matchto、$matchfrom、$matchstandard、$matchLifes\u in、$matchHabiods、$matchskills、$matchspecialization\u 1、$matchdesignation\u 1){返回$query->where($matchfrom)->或where($matchstandard)->或where($matchLifes\u in)->或where($matchHabiods)->或where($matchSkillates)->或where($matchspecialization\u 1)->或wh})->paginate(5);@VArunKumar是的,遗漏了那部分。updateEdit显示未定义的变量$matchto和嵌套内的所有其他变量,如何在嵌套条件内传递这些变量使用($variable)需要::Where('student_id','!=',$id)->Where(函数($query)use($matchto、$matchfrom、$matchstandard、$matchLifes\u in、$matchHabiods、$matchskills、$matchspecialization\u 1、$matchdesignation\u 1){返回$query->where($matchfrom)->或where($matchstandard)->或where($matchLifes\u in)->或where($matchHabiods)->或where($matchSkillates)->或where($matchspecialization\u 1)->或wh})->paginate(5);@VArunKumar是的,错过了那部分。更新