Aman Singh
Aman Singh

Reputation: 17

Array value to check with comma separated values in table and return all row if single match with values

I have a array i.e.

$frame = Array(
   [0]=1,
   [1]=2,
   [2]=4 
);

and I want to get value form table "standard_product" where "frame_category" is a column name, this column value are look like frame_category
4,7
1,3,4
6,7,8
return all row if any value of $frame match with any value of frame_category column. Here require result is:
4,7
1,3,4

My effort to resolve this is:

Standard_product::Where('frame_category', 'LIKE', '%' . $frame . '%')->get()->toArray();
(SELECT * FROM  `standard_products` WHERE  `frame_category` LIKE  "%".$frame."%");

but i will not return expected result. Please help me.

Upvotes: 0

Views: 1652

Answers (3)

Krishna Gupta
Krishna Gupta

Reputation: 685

$frame = Array( [0]=1, [1]=2,[0]=4 );
foreach ($frame as $val) {
$query = mysql_query("SELECT * FROM  `standard_products` WHERE find_in_set(".$val.", frame_category)");
while($row = mysql_fetch_assoc($query)){
$result[] = $row;
}
}

Upvotes: 1

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

remove toArray() last in the query

Standard_product::where('frame_category', 'LIKE', '%'.$frame.'%')->get();

or

Standard_product::like('frame_category', '%$frame%')->get();

Or

$category = DB::table('Standard_product')
          ->where('frame_category', 'like', '%$frame%')
          ->get();

Where Clauses In laravel

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

use MySQL IN

$ids = join(',', $frame);
WHERE  frame_category IN ($ids)

Passing Your to array to MySQL like that

Upvotes: 1

Related Questions