2017-02-21 10 views
0

Запросов:Laravel И, Multiple или красноречивым запрос не работает, как ожидалось

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

дополнительная скобка после student_id`! =? а также. Как я могу достичь этого?

ответ

2

Вы должны гнездиться свое состояние:

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

это показать неопределенную переменную $ matchto и все другие переменные внутри гнезда, как передать эти переменные внутри этого гнезда условие –

+0

получил его использование ($ переменная) Необходимость: : Где ('student_id', '! =', $ Id) \t \t \t \t -> где (функция ($ query) use ($ matchto, $ matchfrom, $ matchstandard, $ matchlives_in, $ matchhobbies, $ matchskills, $ matchspecialization_1, $ matchdesignation_1) { \t \t \t \t \t return $ query-> где ($ matchto) -> orWhere ($ matchfrom) -> orWhere ($ matchstandard) -> orWhere ($ matchlives_in) -> orWhere ($ matchhobbies) -> orWhere ($ matchskills) -> orWhere (matchspecialization_1 $) -> orWhere ($ matchdesignation_1); \t \t \t \t}) -> paginate (5); –

+0

@VArunKumar да, пропущенный эта часть. обновленный – apokryfos