duanjunjie0196 2018-06-09 00:36
浏览 23
已采纳

如何获得每种注册类型的数量?

I have this query:

 public function payment($id = "", $slug = "", $regID)
{
    $registrationTypeDetails = Registration::with(['participants.registration_type',
        'participants' => function ($query) use ($regID) {
            $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
        }
    ])->find($regID);

    return view('conferences.showSummary', compact( 'registrationTypeDetails'));

}

To get the info(name and price) about each registration type where each participant is registered in a specific conference registration. (A registration has some participants and each participant is associated with a registration type.)

So $registrationTypeDetails shows:

Registration {#259 ▼
...
  #relations: array:1 [▼
    "participants" => Collection {#263 ▼
      #items: array:2 [▼
        0 => Participant {#270 ▼
         ...
          #relations: array:1 [▼
            "registration_type" => RegistrationType {#276 ▼
              ....
              #attributes: array:12 [▼
                "id" => 2
                "name" => "free"
                "price" => 0
                "conference_id" => 2
              ]
              ...
            }
          ]
         ...
        }
        1 => Participant {#272 ▼
          ...
          #relations: array:1 [▼
            "registration_type" => RegistrationType {#278 ▼
            ...
              #attributes: array:12 [▼
                "id" => 3
                "name" => "paid"
                "price" => 1
                "conference_id," => 2
              ]
             ...
            }
          ]
        }
      ]
    }
  ]
...
}

With this query results I want to show in the view the registration type name, price, quantity and subtotal. The name and price is already working with the code below.

Doubt: But do you know how to get the quantity and subtotal of each registration type? For example if the registration is composed of 3 participants, 2 participants registered in the registration type "general" and 1 in the registration type "plus" I want to show the quantity 2 for the registration type "general" and quantity 1 for the registration type "plus", like:

  @foreach($registrationTypeDetails->participants as $participant)
<li class="list-group-item list-group-registration d-flex align-items-center justify-content-between">
    <span class="font-size-sm"> {{$participant->registration_type['name']}} </span>
    <span class="font-size-sm">{{$participant->registration_type['price']}} </span>
    <!--<span class="font-size-sm">how to show the quantity? (ex: 2) </span>-->
    <!--<span class="font-size-sm">how to show the subtotal? (ex: 4$)</span>-->
    </li>
@endforeach
  • 写回答

1条回答 默认 最新

  • douhuan6305 2018-06-09 01:54
    关注

    I think I understand: you are trying to show a running total of registration types by participant, and show the sub total count of those registration types as you go down the page. If you don't want to change the loop to loop on the registration type, I think you can do this:

    I would recommend keeping the query you have and create a couple of variables in the controller just for registration types counts, and send that also from the controller to the view.

    $general_type_count = 0;
    $plus_type_count = 0;
    foreach($registrationTypeDetails->participants as $p){
        if($p->registration_type['name'] === 'general')
           $general_type_count ++;
        if($p->registration_type['name'] === 'plus')
           $plus_type_count ++;
    }
    

    Once you have the registration types count in the view, add variables at the top of the loop to count each type as we hit one (this will be the subtotal for the type). This is not great separation of code from the view... but it is pretty bullet proof:

    <?php $plus = 0; $general = 0; ?>
    @foreach($registrationTypeDetails->participants as $participant)
      <li class="list-group-item list-group-registration d-flex align-items-center justify-content-between">
        <span class="font-size-sm"> {{$participant->registration_type['name']}} </span>
        <span class="font-size-sm">{{$participant->registration_type['price']}} </span>
        <span class="font-size-sm">
            @if($participant->registration_type['name'] === 'general')
               {{$general_type_count}}
            @endif
            @if($participant->registration_type['name'] === 'plus')
               {{$plus_type_count}}
            @endif
        </span>
       <?php 
         // Running count in view for subtotals
         if($participant->registration_type['name'] === 'general') 
            $general++;
         if($participant->registration_type['name'] === 'plus') 
            $plus++;
       ?>
        <span class="font-size-sm">{{$$participant->registration_type['name']}}</span>
       // Using a variable variable above to be simpler, but it is basically just calling $general or $plus based on the name of the type
      </li>
    @endforeach
    

    The above is VERY drawn-out, just to explain one way to get what you need; you can make this much tighter for sure. It is also the most basic way of doing this and gets ugly if you have more than a few types of registrations -- you can surely improve this, but I don't know what your controllers look like. If you have some kind of reverse relationship on the participants or types table, or better yet, a many-to-many on the top level Registrations model, perhaps you can do this all in a single query without the loop. Take a look at the withCount() method from laravel as a possibility.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵