Php 雄辩的ORM动态哪里有捷径/更优雅的解决方案?

Php 雄辩的ORM动态哪里有捷径/更优雅的解决方案?,php,orm,laravel,eloquent,Php,Orm,Laravel,Eloquent,以下代码是否有更优雅/更好的解决方案?目前,我不得不重复很多查询,只是为了在查询中添加一个额外的“where” if ($common == true) { $products = self::with(array( 'metal', 'metal.fixes.currency', 'metal.fixes' => function($query) use ($curren

以下代码是否有更优雅/更好的解决方案?目前,我不得不重复很多查询,只是为了在查询中添加一个额外的“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();
    }

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

其次,你不需要一次完成所有的建筑,这里是你可以做到的

PHP5.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();
PHP5.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();

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

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

$query=self::with(数组)(
“金属”,
“金属.修复.货币”,
'metal.fixes'=>函数($query)使用($currency\u id){
$query->where('currency\u id',$currency\u id);
}))
->其中('metal_id',$metal_id)
->其中('product\U type\U id',$product\U type\U id)
如果是($普通){
$query=query->where('common',1);
}
$products=$query->get();
怎么样

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

这更适合于。$query=query->where('common','=',1);应该是$query->where('common','=',1);不需要像这样申请当链接感谢这个$common==true只是为了说明这个问题而编写的。我以前尝试过这个方法,但在链接时遇到了问题。当我已经使用->get()方法时,我正在尝试链接另一个->where