doujiang1832 2017-09-04 13:37
浏览 47

Laravel - 函数where()之后已经检索到模型

Can I use the function where() or some equivalent after the model has already been retrieved? For example:

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

Can I combine these queries into one? Because I have a lot of queries to database per page.

Sorry for my english.

  • 写回答

1条回答 默认 最新

  • dongwen2794 2017-09-04 14:06
    关注

    Please combine your where clause in one function like this

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

    It is much cleaner.

    Yes it is possible to use where function after running a query as long as it returns collection.

    eg You can do

    Equipment::all();
    

    Then apply where clause to the collection it returns. Query like this return an eloquent collection. Go to this documentation page https://laravel.com/docs/5.5/eloquent-collections to see all the functions available for eloquent collection.

    However using where clause to return value of query that end with first() is not possible as it does not return a collection.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?