[英]Convert Mysql Query into Laravel 4 Eloquent
我想將此查詢轉換為Laravel 4 Eloquent。
$query = SELECT * FROM standard_products WHERE frame_category like "%1%" OR
frame_category like "%2%" OR frame_category like "%3%";
我有一個數組
$frame = Array([1]=1,[2]=2,[3]=3);
需求:
使用$frame
數組我希望在Laravel Eloquent中得到相同的結果
我的努力是:
foreach ($frame as $val) {
$match = Standard_product::orWhere('frame_category','like','%'.$val.'%');
}
$match = $match->get()->toArray();
但$match
結果不等於$query
。 請幫幫我
你可以這樣做
Model::where(function ($query) use ($frame) {
foreach($frame as $val){
$query->orWhere('frame_category','like',"%$val%");
}
})->get();
$frame = Array(1,2,3);
$q = DB::table('standard_products');
foreach ($frame as $val) {
$q = $q->orWhere('frame_category','like','%'.$val.'%');
}
產生
select *
from `standard_products`
where `frame_category` like ?
or `frame_category` like ?
or `frame_category` like ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.