简体   繁体   English

Laravel AND,多个或雄辩的查询无法按预期工作

[英]Laravel AND, Multiple OR eloquent query not working as expected

The Query: 查询:

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();

It Produce the Result query like this 它产生这样的结果查询

"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` = ?)"

What i want is 我想要的是

"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` = ?)**)**"

An extra bracket after student_id` != ? 在student_id`!=之后的一个额外的括号。 and. 和。 How can i achieve this? 我怎样才能做到这一点?

You need to nest your condition: 您需要嵌套您的条件:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM