0

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.

3
  • There can't be 2 keys 0 in your array.. Commented Nov 9, 2015 at 10:06
  • Looks like your array has duplicate keys..... which can't exist in PHP Commented Nov 9, 2015 at 10:06
  • Bad database design, but FIND_IN_SET().... otherwise you'll get results for that match 14, 42, etc as well as those that match 4.... and as the db can't use indexes for this type of search, it won't ever be a fast query Commented Nov 9, 2015 at 10:13

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your solution but it will not return required result.
In above example i was got null array as a result.
is database have data??
It will work only if sub string of $frame found in database column
But i need to get all row if any of the value of $frame match with database column
|
1

use MySQL IN

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

Passing Your to array to MySQL like that

Comments

1
$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;
}
}

2 Comments

Thanks for your solution but this is too heavy for me. because it will take much more time to run.
If it takes too long to run, then normalize your database properly and you won't need expensive FIND_IN_SET queries.... you designed your database badly in the first place.... the real fix is to correct the design, not use an inefficient workround

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.