3

I want to convert this query into Laravel 4 Eloquent.

$query = SELECT * FROM standard_products WHERE frame_category like "%1%" OR
frame_category like "%2%" OR frame_category like "%3%";

Here I have an array

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

Requirement:

Using $frame array I want to get same result in Laravel Eloquent

My effort is:

 foreach ($frame as $val) {
     $match = Standard_product::orWhere('frame_category','like','%'.$val.'%');
 }
     $match =  $match->get()->toArray();

But result of $match is not equal to $query. Please help me to

1
  • Could you format your code snippets to make them readable? Commented Nov 13, 2015 at 6:30

2 Answers 2

3

You can do it like as

Model::where(function ($query) use ($frame) {
    foreach($frame as $val){
        $query->orWhere('frame_category','like',"%$val%");
    }
})->get();
Sign up to request clarification or add additional context in comments.

Comments

2
$frame = Array(1,2,3);
$q = DB::table('standard_products');
foreach ($frame as $val) {
    $q = $q->orWhere('frame_category','like','%'.$val.'%');
}

Produces

select *
from   `standard_products`
where  `frame_category` like ?
or     `frame_category` like ?
or     `frame_category` like ?

Comments

Your Answer

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