1
votes

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')";
1
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 :-/ )Rup
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 itPanagiotis Kanavos
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.collins

1 Answers

1
votes

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