[英]Laravel 5: How to verify if values separated by comma from a database table field are contained in an array?
所以,基本上我在數據庫的表中有一個字段,其中包含一些用逗號分隔的值(類似於: value1, value2, value3
)。 我需要以某種方式檢查這些值中的一個或多個是否包含在值數組中並檢索模型。 我想要一個使用Eloquent Model Query來實現這一目標的解決方案,如果模型查詢不可能,那么查詢生成器解決方案也可以,或者可能是替代方案。
試圖在where
語句中使用一個函數,我where
其中循環值數組,並使用like
運算符查詢模型,以檢查字段值是否與數組中的值匹配,最后得到如下內容:
$devices = explode(",", Input::get("devices"));
$products = Product::where(function ($q) use ($devices) {
foreach ($devices as $device) {
$q->orWhere("compatible_devices", "like", "%" . $device . "%");
}
})->get();
到目前為止工作得很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.