【发布时间】:2013-09-05 14:55:42
【问题描述】:
下面的代码有更优雅/更好的解决方案吗?目前,我不得不重复很多查询,只是为了在查询中添加一个额外的“where”。
if ($common == true) {
$products = self::with(array(
'metal',
'metal.fixes.currency',
'metal.fixes' => function($query) use ($currency_id){
$query->where('currency_id', '=', $currency_id);
}))
->where('metal_id', '=', $metal_id)
->where('product_type_id', '=', $product_type_id)
->where('common', '=', 1) // This line is the only difference
between the two Queries
->get();
}
else {
$products = self::with(array(
'metal',
'metal.fixes.currency',
'metal.fixes' => function($query) use ($currency_id){
$query->where('currency_id', '=', $currency_id);
}))
->where('metal_id', '=', $metal_id)
->where('product_type_id', '=', $product_type_id)
->get();
}
【问题讨论】: