检索到第一个商品编号后,我只能从查询中获取所有商品的数据。每个产品都有一个“type_id”,用于确定可以在哪个连接表中找到产品选项。我的问题是如何在“->select()”参数中指定条件以确保每次只检索相应联结表中特定字段的值。
搜索查询如下所示。
public function ProductQuery($queryValue)
{
$product = Product::leftjoin('option_tables_one', 'products.id', '=', 'option_tables_one.product_id')
->leftjoin('option_tables_two', 'products.id', '=', 'option_tables_two.product_id')
->leftjoin('option_tables_three', 'products.id', '=', 'option_tables_three.product_id')
->leftjoin('product_rows_sgsf', 'products.id', '=', 'product_rows_sgsf.product_id')
->select(
'products.*',
'option_tables_one.item_number',
'option_tables_one.sales_price_netto',
'option_tables_one.stock',
'option_tables_two.item_number',
'option_tables_two.sales_price_netto',
'option_tables_two.stock',
'option_tables_three.item_number',
'option_tables_three.sales_price_netto',
'option_tables_three.stock',
)
->where('option_tables_one.item_number', 'LIKE', '%' . $queryValue. '%')
->orWhere('option_tables_two.item_number', 'like', '%' . $queryValue . '%')
->orWhere('option_tables_three.item_number', 'like', '%' . $queryValue . '%')
->get();
return $product;
}
如果产品的“type_id”等于1,那么在“->select()”参数中,只应查询这些字段:
->select(
'products.*',
'option_tables_one.item_number',
'option_tables_one.sales_price_netto',
'option_tables_one.stock',
)
如果产品的“type_id”等于2,那么在“->select()”参数中,只应查询这些字段:
->select(
'products.*',
'option_tables_two.item_number',
'option_tables_two.sales_price_netto',
'option_tables_two.stock',
)
等等...
我有一个更短的查询,它产生与上面看到的相同的结果:
public function ProductQuery($queryValue)
{
$product = ProductOptionTableOne::with('product')->where('item_number', 'LIKE', "%{$queryValue}%")->first();
$product .= ProductOptionTableTwo::with('product')->where('item_number', 'LIKE', "%{$queryValue}%")->first();
$product .= ProductOptionTableThree::with('product')->where('item_number', 'LIKE', "%{$queryValue}%")->first();
return $product;
}
Laravel 的查询构建器不直接支持同一查询中的条件连接或选择,因此您必须根据 type_id 动态构建查询。
使用 addSelect 方法代替 select 以避免覆盖之前添加的 select 语句。这样,您就可以根据条件动态构建您的 select 语句。
public function ProductQuery($queryValue)
{
$query = Product::query();
// Add joins and selects conditionally based on type_id
$query->when($type_id == 1, function ($q) {
return $q->leftJoin('option_tables_one', 'products.id', '=', 'option_tables_one.product_id')
->addSelect('option_tables_one.item_number', 'option_tables_one.sales_price_netto', 'option_tables_one.stock');
});
$query->when($type_id == 2, function ($q) {
return $q->leftJoin('option_tables_two', 'products.id', '=', 'option_tables_two.product_id')
->addSelect('option_tables_two.item_number', 'option_tables_two.sales_price_netto', 'option_tables_two.stock');
});
// Add more conditions
// Add the base product fields and the where conditions
$query->addSelect('products.*')
->where(function ($q) use ($queryValue) {
$q->where('option_tables_one.item_number', 'LIKE', '%' . $queryValue . '%')
->orWhere('option_tables_two.item_number', 'LIKE', '%' . $queryValue . '%')
// Add more orWhere conditions for other tables if needed
;
});
// Get the results
return $query->get();
}