1

Is there a more elegant / better solution to the below code? At present, I have to repeat a lot of query just to add an extra 'where' into the query.

    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();
    }
4

3 回答 3

4

首先你为什么要这样做$common == true

其次,您不需要一次完成所有构建,这是您可以做到的。

PHP 5.3

$products = $this->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);

if ($common)
{
    $products->where('common', 1);
}

$products = $products->get();

PHP 5.4

$products = $this->with(
          ['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);

if ($common)
{
    $products->where('common', 1);
}

$products = $products->get();

可以更好地格式化,但你明白了。

于 2013-09-05T15:02:30.130 回答
0

Sinque Eloquent/QueryBuilder 总是返回对自身的引用,你可以编写一个更优雅的版本,如下所示:

$query = 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)

if ($common) {
    $query = query->where('common', 1);        
}

$products = $query->get();
于 2013-09-05T15:02:38.343 回答
-2

怎么样

$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', '=', (int)$common) // This line is the only difference 
                                            between the two Queries
           ->get();           
}

这样你就不需要if。如果你必须不关心通用标志

$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);

$products = ($common) ? $products->where('common', 1)->get() : $products->get();
于 2013-09-05T15:04:34.477 回答