您知道如何正确地将此代码从视图移动到控制器吗?



>我在下面有这个代码来显示注册的摘要,例如:

Registration type        Quantity            Price           Subtotal
general                     1                0.00             0.00
plus                        2                2.00             4.00

视图中用于显示摘要的代码:

<ul>
<li>
<span>Registration Type</span>
<span>Quantity</span>
<span >Price</span>
<span>Subtotal</span>
</li>
<?php
$total = 0;
$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)
@php ($total +=$participant->registration_type->price * $type_counts[$name])
<li>
<span>{{$name}}</span>
<span>{{$type_counts[$name]}}</span>
<span>{{ number_format($participant->registration_type->price, 2)}}$</span>
<span>{{ number_format($participant->registration_type->price * $type_counts[$name], 2)}}$</span>
</li>
@endforeach
<li>
<span>TOTAL</span>
<span>{{ number_format($total, 2)}}€</span>
</li>
</ul>

它工作正常,但我想将上面的 php 代码传递给控制器 showSummary(( 方法,然后将必要的数据返回到视图以显示摘要,但我没有成功实现这一点。

您知道如何正确实现这一点吗?

我在控制器中有showSummary()方法,该方法将"注册类型详细信息"返回到上述视图。

public function showSummary($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', 'id', 'slug'));
}

我为此所做的是将零件放入控制器中以计算每种注册类型的数量,例如:

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);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
$registrationTypes = [];
return view('conferences.payment', compact( 'type_counts', 'registrationTypeDetails', 'id', 'slug'));
}

在观点中只是:

<ul>
<li>
<span>Registration Type</span>
<span>Quantity</span>
<span>Price</span>
<span>Subtotal</span>
</li>
<?php
$total = 0;
?>
@foreach($registrationTypeDetails->participants as $participant)
@php ($name = $participant->registration_type->name)
@php ($total +=$participant->registration_type->price * $type_counts[$name])
<li>
<span>{{$name}}</span>
<span>{{$type_counts[$name]}}</span>
<span>{{ number_format($participant->registration_type->price, 2)}}
€</span>
<span>{{ number_format($participant->registration_type->price * $type_counts[$name], 2)}}
€</span>
</li>
@endforeach
<li>
<span>TOTAL</span>
<span>{{ number_format($total, 2)}}€</span>
</li>
</ul>

但是,我不知道这是否可以,如果有更多的代码应该转到控制器。因为我需要在会话中存储注册的总值($total(,但总值($total(在视图中而不是在控制器中,这似乎不正确。

答案是两者都不是。

控制器用于将数据从一个位置传递到另一个位置,视图用于显示数据。一个好的规则是,如果你正在编写PHP并在Blade模板中创建数组,那么你做得太多了(听起来你已经意识到了(。

在应用中创建一个名为repositories的文件夹,并创建一个RegistrationRepository或类似的东西。将您的表格构建为收藏 - 或者以更有效的方式从 Eloquent 获取它。然后使用集合map()创建总计。将该数据传递到视图中,然后简单地循环遍历并显示它。

保持模型和控制器的轻量级,创建新类以保留复杂的逻辑。还有一种观点认为,这可以作为视图编辑器来完成,但我认为当您创建额外的信息并从几个地方阅读时,为其创建存储库是最干净的。

然而,这是一种意见,而不是答案。遗憾的是,没有一个明确的答案,因为MVC是你想要的。Rails喜欢重型模型,CodeIgniter喜欢重型控制器。个人我相信Laravel是关于拥有光控制器和灯光模型 - 但这由你决定是否同意。

最新更新