我正在尝试开发类似于图像中的上下文。
所以对于"屏幕1"我有一个" single.blade.php"与此路线相关联的文件:
// Route for congress details page:
Route::get('/congress/{id}/{slug?}', [
'uses' => 'FrontController@show',
'as' =>'congresses.show'
]);
因此,当用户acesses" 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'
]);
当用户点击&#34;下一步&#34;提交表单,并且必须由用户存储每种票证类型的选定数量。并且还需要从DB获取一些信息,然后重定向到registration.blade.php页面。我创建了一个RegistrationController来处理它。
然后,在图像的屏幕2中,必须在第一步中显示用户所选量子的摘要,并且还需要显示登记表。
注册表格:
当用户点击&#34;下一步&#34;时,开发此上下文在图像的第一个屏幕中,在congres详细信息页面中,RegistrationController具有storeQuantity()
方法:
所以我在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用于用户选择支付类型。步骤3是用户引入支付数据。要在表单之间导航,我有一些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>
怀疑和问题:
与此问题背景相关的表格关系:
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');
}
}
答案 0 :(得分:3)
给出
public function storeQuantity(Request $request, $id){
...
$customQuestionsOfTicketType = $type->questions;
...
}
在每个周期覆盖customQuestionsOfTicketType
的值。相反,它可以是$selectedTpyes[$type->name]
的属性,如:
$selectedTpyes[$type->name]['questions'] = $type->questions;
然后在视图中,您将循环$selectedTpyes[$type->name]
而不是$customQuestions
。
您将视图中的参与者列表(allParticipants
)和类型列表(selectedTypes
)传递给视图,这是两个独立的实体,我认为这很好。通过上面的解决方案,您可以摆脱$customQuestionsOfTicketType
。
我不会在这里看到大问题,除了我将信息检索与控制器分开,将其移动到一个独立的类,这将是控制器的依赖。
您可以通过dependency injection注入这个新课程。
您可以使用存储库模式来访问数据库资源。如果您对此不满意,可以参加以下几个讲座:
以及可以帮助您的Laravel套餐:
这是一个应用程序设计问题,这基本取决于你。鉴于上下文,我将/step1
显示某些表单字段的步骤分开,/step2
详细说明了第1步数据并返回了更多字段,依此类推......