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

时间:2018-06-09 00:36:23

标签: php laravel

我有这个问题:

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

}

获取每个参与者在特定会议注册中注册的每种注册类型的信息(名称和价格)。 (注册有一些参与者,每个参与者都与注册类型相关联。)

所以$ registrationTypeDetails显示:

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
              ]
             ...
            }
          ]
        }
      ]
    }
  ]
...
}

使用此查询结果,我想在视图中显示注册类型名称,价格,数量和小计。名称和价格已经使用下面的代码。

怀疑:但是您知道如何获得每种注册类型的数量和小计吗?例如,如果注册由3个参与者组成,则2个参与者在注册类型“general”中注册,1个注册注册类型  “plus”我想显示注册类型“general”的数量2和注册类型“plus”的数量1,如:

  @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 个答案:

答案 0 :(得分:1)

我想我明白了:您正试图通过参与者显示正在运行的注册类型总数,并在页面下方显示这些注册类型的子总数。如果您不想将循环更改为循环注册类型,我认为您可以这样做:

我建议保留您的查询,并在控制器中创建几个变量,仅用于注册类型计数,并将其也从控制器发送到视图。

$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 ++;
}

在视图中注册类型计数后,在循环顶部添加变量,以便在我们点击一​​个时计算每个类型(这将是该类型的小计)。这不是代码与视图的完美分离......但它非常具有防弹性能力:

<?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

以上内容非常简洁,只是为了解释一种获得所需信息的方法;你可以肯定这么紧。这也是最基本的做法,如果你有多种类型的注册就会变得很难看 - 你可以肯定地改进这一点,但我不知道你的控制器是什么样的。如果您在参与者或类型表上有某种反向关系,或者更好的是,在顶级注册模型中有多对多关系,也许您可​​以在没有循环的单个查询中完成所有操作。请看withCount() method from laravel作为一种可能性。