大佬教程收集整理的这篇文章主要介绍了php-如何更好地构造此上下文,并为每个选定的票证类型显示与该票证类型关联的自定义字段?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试开发图像中的上下文.
因此,对于图像的“屏幕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 }}]">
具有以下操作和路线:
//形成动作
<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'
]);
当用户单击“下一步”时,将提交表单,该表单是存储用户为每种票证类型选择的数量所必需的.并且还需要从数据库获取一些信息,然后重定向到registration.blade.PHP页面.我创建了一个RegistrationController来处理这个问题.
然后,在图像的屏幕2中,有必要在第一步中显示用户选择的数量的摘要,并且还需要显示注册表格.
注册表:
>始终在正在注册的用户的“买方信息”部分中介绍其姓名,姓氏和电子邮件.
>如果大会已支付机票类型,则还有“计费信息”部分.
>如果会议表的列“ collect_info_from_all_participants”的值为“ 1”,则应针对用户选择的每种票证类型显示一个区域,以收集每个所选票证的参与者的姓名和姓氏.但是,每种票证类型都可以关联自定义问题.例如,图像中的票证类型“完全访问”具有定制问题“电话”,而票证类型“基本”没有任何定制问题.因此,有必要显示与每种票证类型关联的自定义问题.
为了在用户单击图像的第一个屏幕中的“下一步”时在congres详细信息页面中开发此上下文,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;
$@R_130_10586@l = 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]['sub@R_130_10586@l'] = $price * $quantity;
$@R_130_10586@l+= $SELEctedTypes[$type->name]['sub@R_130_10586@l'];
$SELEctedTypes[$type->name]['@R_130_10586@l'] = $@R_130_10586@l;
}
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['sub@R_130_10586@l'], 2)}}€</span>
</li>
@endforeach
然后,我有一个多步骤注册表格.第一步是注册表.步骤2供用户选择付款类型.步骤3用于用户介绍支付数据.为了在表单之间导航,我有一些jquery,但应该与问题无关.
我的怀疑主要是关于第一步.
<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>
疑问和问题:
>使用此代码,将针对所有票证类型显示会议存在的自定义问题.但是,自定义问题仅应针对与该自定义问题相关联的票证类型出现.
>通过此代码,Im将许多数组传递给视图,但这似乎并不正确,您知道如何仅准备一个具有所有必要数据的数组吗?
>同样,使用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');
}
}
解决方法:
>问:使用此代码,对于所有票证类型,都会出现会议存在的自定义问题.但是,自定义问题仅应针对与该自定义问题相关联的票证类型出现.
给定
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.
>问:同样,使用RegistrationController的storequantity方法执行所有操作并返回POST请求的视图时,代码流似乎也不正确.您知道开发这种环境是否有更好的流程吗?由于这是一个多步骤的形式,也许有更好的方法来组织上下文.
我没有在这里看到大问题,除了我将信息检索与Controller分开,将其移到一个单独的类中,这将是Controller的依赖项.
您可以通过dependency injection注入这个新类.
您可以使用存储库模式来访问数据库资源.如果您不满意,这里有一些讲座:
> http://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html
> http://shawnmc.cool/the-repository-pattern
和Laravel套件可以帮助您:
> https://github.com/andersao/l5-repository
问:另外,我是否有必要在每个步骤中都有一个表单,或者只是一个全局表单.第一步是收集有关正在注册的用户和其他参与者的信息.第二步是选择付款方式.第三步是介绍支付数据和支付方式.但是,如果用户选择的付款方式不是即时的,则在第三步中,代替用户介绍付款数据,它会向用户显示一些生成的代码,以便用户进行付款.我怀疑如何根据必要的形式和必要的路线来组织这一工作.
这是一个应用程序设计问题,基本上由您决定.给定上下文,我将/ step1显示一些表单字段的步骤分开了,/ step2详细说明了步骤1的数据并返回了其他字段,依此类推…
以上是大佬教程为你收集整理的php-如何更好地构造此上下文,并为每个选定的票证类型显示与该票证类型关联的自定义字段?全部内容,希望文章能够帮你解决php-如何更好地构造此上下文,并为每个选定的票证类型显示与该票证类型关联的自定义字段?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。