【发布时间】:2018-09-20 16:37:41
【问题描述】:
我正在尝试开发一个类似于图像中的上下文。
所以对于图像的“屏幕 1”,我有一个与此路由关联的“single.blade.php”文件:
// Route for congress details page:
Route::get('/congress/{id}/{slug?}', [
'uses' => 'FrontController@show',
'as' =>'congresses.show'
]);
因此,当用户访问“http://project.test/congress/1/congress-title-test/”时,他将访问大会详细信息页面。在此页面中,除了大会详细信息外,还有一个表格供用户选择每种票证类型所需的数量。
此表单具有此名称的选择菜单:
<select name="types[{{ $type->name }}]">
An 有这个动作和路线:
// 表单动作
<form method="post" action="{{route('congresses.storeQuantities', ['id' => $congress->id, 'slug' => $congress->slug])}}">
//表单路由:
Route::post('/congress/{id}/{slug?}/registration', [
'uses' => 'RegistrationController@storeQuantity',
'as' =>'congresses.storeQuantities'
]);
当用户单击“下一步”时,表单被提交,并且需要存储用户为每种票类型选择的数量。并且还需要从 DB 中获取一些信息,然后重定向到 registration.blade.php 页面。我创建了一个 RegistrationController 来处理这个问题。
然后,在图像的屏幕 2 中,需要在第一步中显示用户所选数量的摘要,并且还需要显示注册表。
报名表:
- 总是有一个“买家信息”部分供注册的用户介绍他的姓名和电子邮件。
- 如果大会有付费票类型,还有“计费信息”部分。
- 如果大会表的“collect_info_from_all_participants”列的值为“1”,则它应该为用户选择的每个票证类型显示一个部分,用于收集每个所选票证的参与者的姓名和姓氏。但是,每种工单类型都可以关联自定义问题。例如,如图所示,工单类型“完全访问”具有自定义问题“电话”,工单类型“基本”没有任何自定义问题。因此,有必要显示与每种工单类型相关的自定义问题。
当用户在图像的第一个屏幕中单击“下一步”时开发此上下文,在大会详细信息页面中,RegistrationController 具有storeQuantity() 方法:
- 存储选定的数量
- 检查大会是否有值为“1”的“collect_info_from_all_participants”列
- 为每个选定的工单类型获取与该工单类型关联的自定义问题
- 将所有这些内容返回到registration.blade.php 视图。
所以我在 RegistrationController 中有 storeQuantity 方法:
class RegistrationController extends Controller
public function storeQuantity(Request $request, $id, $slug=null){
$typeQuantities = $request->get('types');
$allParticipants = Congress::where('id', $id)->first()->all_participants;
$total = 0;
foreach($typeQuantities as $typeName => $quantity){
$type = TicketType::where('name', $typeName)->firstOrFail();
$price = $type->price;
$customQuestionsOfTicketType = $type->questions;
$selectedTypes[$type->name]['quantity'] = $quantity;
$selectedTypes[$type->name]['price'] = $price;
$selectedTypes[$type->name]['subtotal'] = $price * $quantity;
$total+= $selectedTypes[$type->name]['subtotal'];
$selectedTypes[$type->name]['total'] = $total;
}
Session::put('selectedTypes', $selectedTypes);
Session::put('allParticipants' , $allParticipants);
Session::put('customQuestionsOfTicketType' , $customQuestionsOfTicketType);
return redirect(route('congresses.registration',['id' => $id, 'slug' => $slug]));
}
public function displayRegistrationPage(Request $request, $id, $slug=null){
$selectedTypes = Session::get('selectedTypes');
$allParticipants = Session::get('allParticipants');
return view('congresses.registration', ['selectedTypes' => $selectedTypes, 'allParticipants' => $allParticipants]);
}
}
// displayRegistrationPage的路由
Route::get('/congress/{id}/{slug?}/registration', [
'uses' => 'RegistrationController@displayRegistrationPage',
'as' =>'congresses.registration'
]);
在registration.blade.php(图像的屏幕2)中,首先我有显示所选票证类型、小计、总计的摘要:
@foreach($selectedTypes as $k=>$selectedType)
<li class="list-group-item">
<span>{{$k}}</span>
<span>{{$selectedType['quantity']}}</span>
<span>{{ number_format($selectedType['price'], 2)}}€</span>
<span>{{ number_format($selectedType['subtotal'], 2)}}€</span>
</li>
@endforeach
然后我有一个多步骤注册表单。第一步是登记表。第 2 步是让用户选择支付类型。第三步,用户介绍支付数据。要在表单之间导航,我有一些 jquery,但不应该与问题相关。
我的疑惑主要是第一步。
Registration.blade.php 多步表单:
<div class="registration_form">
<ul class="nav nav-pills" role="tablist">
<li class="">
<a class="nav-link active" href="#step1" data-toggle="tab" role="tab">Step 1 - Registration Information</a>
</li>
<li class="disabled">
<a class="nav-link" href="#step2" data-toggle="tab" role="tab"> Step 2 - Payment Methods</a>
</li>
<li class="disabled">
<a class="nav-link" href="#step3" data-toggle="tab" role="tab"> Step 3 - Payment</a>
</li>
</ul>
<form method="post">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="step1" role="tabpanel" aria-labelledby="home-tab">
<h6>Buyer Information</h6>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
</div>
<div class="form-group">
<label for="surname">Surname</label>
<input type="text" class="form-control" name="surname" id="surname" value="{{ (\Auth::check()) ? Auth::user()->surname : old('surname')}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="emai" id="email" value="{{ (\Auth::check()) ? Auth::user()->email : old('email')}}">
</div>
<!-- If the congress has paid ticket types -->
@if( array_sum(array_column($selectedTypes, 'price')) > 0 )
<h6>Billing Information</h6>
<div class="form-group font-size-sm">
<label for="inputName">Name</label>
<input type="text" class="form-control" id="inputName">
</div>
<div class="form-group font-size-sm">
<label for="inputName">Country</label>
<input type="text" class="form-control" id="inputName">
</div>
<!-- ... -->
@endif
<!-- If its necessary to collect data from all participants -->
@if (!empty($allParticipants))
@if($allParticipants == 1)
@foreach($selectedTypes as $k=>$selectedType)
<h6>Participant - 1 - {{$k}}</h6>
<div class="form-group font-size-sm">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" value="">
</div>
<div class="form-group font-size-sm">
<label for="surname">Surname</label>
<input type="text" class="form-control" name="surname" id="surname" value="">
</div>
<!-- If the ticket type has custom questions show the questions and if the are required add the required property -->
@if (!empty($customQuestions))
@if(count($customQuestions) > 0)
@foreach($customQuestions as $customQuestion)
<div class="form-group">
<label for="test">{{$customQuestion->question}}</label>
<input type="text" class="form-control" name="" id="" value="">
</div>
@endforeach
@endif
@endif
@endforeach
@endif
@endif
<button type="button" href="#step2" data-toggle="tab" role="tab" class="btn btn-primary next-step">
GO to Step 2
</button>
</div>
<div class="tab-pane fade clearfix" id="step2" role="tabpanel" aria-labelledby="profile-tab">
<form method="post">
<h6>Select the payment method</h6>
<!-- radio buttons fields -->
<button type="button" href="#step3" data-toggle="tab" role="tab" class="btn btn-primary next-step"> Go to step 3 </button>
</form>
</div>
<div class="tab-pane clearfix fade" id="step3" role="tabpanel" aria-labelledby="contact-tab">
<form method="post">
<h6>Payment</h6>
<!-- payment fields -->
<button type="button" href="#step3" data-toggle="tab" role="tab" class="btn btn-primary next-step"> Confirm </button>
</form>
</div>
</div>
</form>
</div>
疑问和问题:
- 使用此代码,针对大会存在的自定义问题会出现在所有票证类型中。但自定义问题应仅针对与该自定义问题关联的工单类型显示;
- 使用此代码我将许多数组传递给视图,但这似乎不太正确,您知道如何只准备一个包含所有必要数据的数组吗?
- 另外,代码流似乎不太正确,RegistrationController 的 storeQuantity 方法做了所有事情。您知道是否有更好的流程来开发此上下文?由于这是一个多步骤形式,也许有更好的方法来组织上下文。
- 我还知道是否有必要在每个步骤中使用表单或仅使用全局表单。第一步是收集有关正在注册的用户和其他参与者的信息。第二步,选择支付方式。第三步,引入支付数据并支付。但是如果用户选择的支付方式不是即时的,在第三步中,用户不会引入支付数据,而是向用户提供一些生成的代码,以便用户进行支付。我对如何根据必要的形式和必要的路线来组织这一点感到怀疑。
与此问题上下文相关的表关系:
1 to many between congress and registration (a congress can have many registrations)
1 to many between registration and participants (a registration can have man participants)
1 to many between congress and ticket types (a congress can have many ticket types)
1 to many between ticket types and ticket_type_questions (a ticket type can have many custom questions)
1 to many between questions and ticket_type_questions (a question can be associated with many ticket types)
// TicketType Model
class TicketType extends Model
{
protected $fillable = [
'name', '...', 'congress_id'
];
public function congress(){
return $this->belongsTo('App\Congress');
}
public function questions(){
return $this->belongsToMany('App\Question', 'ticket_type_questions');
}
}
// Question Model
class Question extends Model
{
protected $fillable = [
'question', 'congress_id',
];
public function ticket_types(){
return $this->belongsToMany('App\TicketType', 'ticket_type_questions');
}
}
【问题讨论】: