2017-09-04 48 views
0

我可以使用函数,其中()或其他等效模型已被检索到? 例如:Laravel - 函数where()已经检索到模型后

$head = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0) 
         ->whereHas('item', function ($q) { 
          $q->where('type_id', 1); 
         })->where('wear', 1)->first(); 

    $torso = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 2); 
    })->where('wear', 1)->first(); 

    if ($torso == null) 
     $torso = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
      $q->where('type_id', 3); 
     })->where('wear', 1)->first(); 

    $hands = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 4); 
    })->where('wear', 1)->first(); 

    $waist = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 5); 
    })->where('wear', 1)->first(); 

    $feet = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 6); 
    })->where('wear', 1)->first(); 

    $amulet = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 8); 
    })->where('wear', 1)->first(); 

    $first_ring = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 9); 
    })->where('wear', 1)->first(); 

    $secound_ring = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->where('type_id', 9); 
    })->where('wear', 2)->first(); 

    $right_hand = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) { 
     $q->whereIn('type_id', [10, 11]); 
    })->where('wear', 1)->first(); 

能否这些查询合并成一个?因为每页数据库都有很多查询。

对不起,我的英语。

回答

1

请结合您的where子句中的一个函数这样

$torso = Equipment::where([['user_id', $fighter->id], ['wear', 1]])->first(); 

它是干净多了。

是的,只要返回集合,就可以在运行查询后使用where函数。

如 你可以做

Equipment::all(); 

然后where条款适用于它返回集合。 像这样的查询返回eloquent collection。 转到此文档页面https://laravel.com/docs/5.5/eloquent-collections查看可用于雄辩集合的所有功能。

但是使用where子句以first()结尾的查询返回值是不可能的,因为它不返回集合。

相关问题