user2680289
user2680289

Reputation:

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` != ? and. How can i achieve this?

Upvotes: 0

Views: 304

Answers (1)

apokryfos
apokryfos

Reputation: 40681

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

Upvotes: 2

Related Questions