我正在构建Laravel 5.4 Web应用程序,我在数据库表下面:
==================================
product_id|attribute_id|option_id
==================================
1 | 1 | 1
1 | 2 | 3
1 | 4 | 10
2 | 1 | 1
2 | 2 | 4
... etc
所以我提交带有属性id和选项id的表单,这样我就可以从它或其他任何地方构建数组。
我希望从数据库中选择匹配完全组合的product_id,例如:
[
attribute_id => 1,
option_id => 1
attribute_id => 2,
option_id => 3
attribute_id => 4,
option_id => 10
]
此条件仅适用于product_id = 1
的产品不知道我是否可以使用数据库查询或php。
答案 0 :(得分:1)
首先,制作一个反映您数据的模型。然后使用Eloquent查询构建器获取您要查找的数据。如果您只需要返回一个匹配的数字,请务必在查询结尾处添加" - > distinct()"。
您也可以将一系列条件传递给'其中'子句。
您的代码可能如下所示:
$match = DB::table('products')
->where('attribute_id', 1)
->where('option_id', 1)
->distinct()
->get();
答案 1 :(得分:1)
如果您只想要product_id = 1的产品 假设您已将其存储在“product_attribute_option”表中 它的字段是product_id | attribute_id |你显示的option_id。
$query = DB::table('product_attribute_option as pao');
$query->where('pao.product_id', 1);
$results = $query->get();