session 中的注册有 1 个或多个关联的参与者,并且与注册关联的每个参与者都与注册类型相关联。
我有一个查询来获取与特定注册参与者关联的每个注册类型的信息(名称和价格),查询结果是:
Registration {#258 ▼
...
#relations: array:1 [▼
"participants" => Collection {#262 ▼
#items: array:2 [▼
0 => Participant {#269 ▼
#fillable: array:4 [▶]
...
#relations: array:1 [▼
"registration_type" => RegistrationType {#274 ▶}
]
... // then RegistrationType has attributes: name, price, etc
}
1 => Participant {#271 ▼
#fillable: array:4 [▶]
...
#relations: array:1 [▼
"registration_type" => RegistrationType {#274 ▼
... // then RegistrationType has attributes: name, price, etc
}
]
}
]
}
]
...
}
现在我想显示注册摘要,例如:
Registration type Quantity Price Subtotal
general 1 0.00 0.00
plus 2 2.00 4.00
我有下面的代码来获取此摘要,但它使用静态注册类型名称(一般“和加号”)。但是注册类型可以有任何名称,创建 session 的用户可以使用任何名称创建注册类型. 您知道如何使用任何注册类型名称而不只是静态值“general”和“plus”来实现相同的摘要吗?
<ul class="list-group">
<li>
<span>Registration Type</span>
<span>Quantity</span>
<span>Price</span>
<span>Subtotal</span>
</li>
<?php
$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 ++;
}
$plus = 0; $general = 0;
?>
@foreach($registrationTypeDetails->participants as $participant)
<li>
<span> {{$participant->registration_type['name']}} </span>
<span>
@if($participant->registration_type['name'] === 'general')
{{$general_type_count}}
@endif
@if($participant->registration_type['name'] === 'plus')
{{$plus_type_count}}
@endif
</span>
<?php
if ($participant->registration_type['name'] === 'general')
$general++;
if ($participant->registration_type['name'] === 'plus')
$plus++;
?>
<span class="font-size-sm">{{$participant->registration_type['price']}} </span>
<span class="font-size-sm">{{$participant->registration_type['price'] * $general_type_count}} </span>
</li>
@endforeach
</ul>
返回“registrationTypeDetails”的 Controller :
class PController extends Controller
{
public function showSummary($id = "", $slug = "", $regID)
{
$registrationDetails = Registration::with('participants:id,ticket_type_id,registration_id')->find($registrationID);
return view('conferences.showSummary', compact(registrationTypeDetails'));
}
}
最佳答案
您可以使用数组代替变量:
<ul class="list-group">
<li>
<span>Registration Type</span>
<span>Quantity</span>
<span>Price</span>
<span>Subtotal</span>
</li>
<?php
$type_counts = [];
foreach($registrationTypeDetails->participants as $p){
if (!isset($type_counts[$p->registration_type['name']])) {
$type_counts[$p->registration_type['name']] = 0;
}
$type_counts[$p->registration_type['name']]++;
}
?>
@foreach($registrationTypeDetails->participants as $participant)
<li>
<span> {{$participant->registration_type['name']}} </span>
<span>
{{$type_counts[$participant->registration_type['name']]}}
</span>
<span class="font-size-sm">{{$participant->registration_type['price']}} </span>
<span class="font-size-sm">{{$participant->registration_type['price'] * $type_counts[$participant->registration_type['name']]}} </span>
</li>
@endforeach
</ul>
我删除了未使用的$general
和plus
。如果需要,您可以为他们做同样的事情。
为 $type_counts[$participant->registration_type['name']]
使用一个变量也是一个好主意,它经常被使用。这样可以避免很多重复:
<ul class="list-group">
<li>
<span>Registration Type</span>
<span>Quantity</span>
<span>Price</span>
<span>Subtotal</span>
</li>
<?php
$type_counts = [];
foreach($registrationTypeDetails->participants as $p){
$name = $p->registration_type['name'];
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
?>
@foreach($registrationTypeDetails->participants as $participant)
@php ($name = $participant->registration_type['name'])
<li>
<span> {{$name]}} </span>
<span>
{{$type_counts[$name]}}
</span>
<span class="font-size-sm">{{$participant->registration_type['price']}} </span>
<span class="font-size-sm">{{$participant->registration_type['price'] * $type_counts[$name]}} </span>
</li>
@endforeach
</ul>
关于php - 如何更改代码,使其不仅适用于静态值 "general"和 "plus"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779708/