1

I need help converting this query to a laravel query.

$sql = "SELECT*FROM `shelf` WHERE(column1 = '$col1' OR column2 = '$col1' OR column3 = '$col1' OR column4 = '$col1' OR column4 = '$col1')";
$sql = $sql."AND(column1 = '$col2' OR column2= '$col2' OR column3= '$col2' OR column4= '$col2' OR column5= '$col2')";
$sql = $sql."AND(column1 = '$col3' OR column2 = '$col3' OR column3 = '$col3' OR column4 = '$col3' OR column5 = '$col3')";
3
  • What specifically are you stuck on? The combination of ANDing groups of ORs? (I can't see how to do that at first glance either :-/ ) Commented Apr 27, 2018 at 10:15
  • Not my downvote but ... That's a bad SQL query for multiple reasons. First, it's wide open to SQL injection attacks and conversion errors. Passing a non-ASCII character will even lead to mangled data. Second, it's too complicated - is there a real need to search using all column values? Or could this be replaced with an IN (value1,value2,value3) clause ? What is this trying to do? Simplifying the query will make it a lot easier to convert it Commented Apr 27, 2018 at 10:16
  • The query is suppose to return a row that contains variable 1, 2 and 3. But i want the query to converted to laravel query. Commented Apr 27, 2018 at 10:24

1 Answer 1

1

It's strange query but in laravel way it looks like this:

\DB::table('shelf')->where(function($query) use ($col1){
       $query->where('column1', $col1)->orWhere('column2', $col1)->orWhere('column3', $col1)->orWhere('column4', $col1)->orWhere('column5', $col1);
   })->where(function($query) use ($col2){
       $query->where('column1', $col2)->orWhere('column2', $col2)->orWhere('column3', $col2)->orWhere('column4', $col2)->orWhere('column5', $col2);
   })->where(function($query) use ($col3){
       $query->where('column1', $col3)->orWhere('column2', $col3)->orWhere('column3', $col3)->orWhere('column4', $col3)->orWhere('column5', $col3);
   })->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.