php - 如何更好地构造此上下文,并为每个选定的票证类型显示与该票证类型关联的自定义字段?

标签 php laravel

我试图建立一个像在图像中一样的上下文。
enter image description here
因此,对于图像的“screen 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动作
<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页面。我为处理这个创建了一个注册控制器。
然后,在图像的屏幕2中,需要在第一步骤中由用户显示所选择的数量的摘要,并且还需要显示登记表。
登记表:
始终有一个“买家信息”部分为正在进行注册的用户介绍他的姓名、姓氏和电子邮件。
如果国会已经支付了票款,还有“账单信息”部分。
如果会议表中有“Collect_info_from_all_particients”列,值为“1”,则应为用户选择的每种票证类型显示一个部分,以收集每个选定票证的参与者的姓名和姓氏。但是,每个票类型都可以有相关的自定义问题。例如,在图像中,票证类型“完全访问”有自定义问题“电话”,票证类型“基本”没有任何自定义问题。因此,有必要显示与每种票证类型相关的自定义问题。
要在用户在图像的第一个屏幕中单击“下一步”时开发此上下文,在“一致详细信息”页中,RegistrationController具有storeQuantity()方法:
存储所选数量
检查大会是否有“向所有参与者收集信息”列,值为“1”
对于每个选定的票证类型,获取与该票证类型关联的自定义问题
将所有这些内容返回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是让用户选择付款类型。步骤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>

疑问和问题:
有了这个代码,所有类型的票都会出现国会存在的自定义问题。但自定义问题只应出现在与该自定义问题相关联的票证类型中;
有了这段代码,我向视图传递了许多数组,但这似乎不太正确,你知道如何只有一个数组准备好所有必要的数据吗?
另外,registrationcontroller的storequantity方法似乎不太正确。你知道是否有更好的流程来开发这个上下文吗?因为这是一个多步骤的形式,也许有更好的方法来组织上下文。
此外,我还有关于是否有必要在每个步骤中都有一个表单,或者只是一个全局表单。第一步是收集正在进行注册的用户和其他参与者的信息。第二步是选择付款类型。第三步是介绍支付数据和支付。但是如果用户选择了一个不立即支付的METoHD,在第三步而不是用户引入支付数据时,它向用户呈现生成的一些代码,这样用户就可以支付。我对如何从必要的形式和必要的途径来组织这件事有疑问。
与此问题上下文相关的表关系:
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请求的视图,代码流看起来也不太正确。你知道是否有更好的流程来开发这个上下文吗?因为这是一个多步骤的形式,也许有更好的方法来组织上下文。
我不认为这里有什么大问题,除了我将信息检索从控制器中分离出来,将其移动到一个单独的类中,这将是控制器的一个依赖项。
您可以通过dependency injection注入这个新类。
您可以使用存储库模式来访问数据库资源。如果你对此不满意,这里有几节课:
http://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html
http://shawnmc.cool/the-repository-pattern
还有一个可以帮助你的大包裹:
https://github.com/andersao/l5-repository
问:我还有一个问题,就是每一步是否需要一个表单,或者只是一个全局表单。第一步是收集正在进行注册的用户和其他参与者的信息。第二步是选择付款类型。第三步是介绍支付数据和支付。但是如果用户选择了一个不立即支付的METoHD,在第三步而不是用户引入支付数据时,它向用户呈现生成的一些代码,这样用户就可以支付。我对如何从必要的形式和必要的途径来组织这件事有疑问。
这是一个应用程序设计问题,基本上由您决定。考虑到上下文,我将分离/step1显示一些表单字段的步骤,/step2详细说明步骤1数据并返回更多字段,等等……

关于php - 如何更好地构造此上下文,并为每个选定的票证类型显示与该票证类型关联的自定义字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764128/

相关文章:

php - Laravel catch Eloquent "Unique"字段错误

php - 迁移: Cannot declare class X,时发生错误,因为该名称已被使用

javascript - 如何在 angularJS 中创建座位表

javascript - Vue.js 中的财务计算 - 使用计算方法

PHP Web 服务优化和测试方法

php - JSON 解码不起作用

php - HTML 和 PHP 简单联系表

php - 在 Mac 上搭建 MAMP 环境……最佳方案?

php - laravel 4:为什么 Request::header() 没有得到指定的 header ?

php - Laravel Firebase cURL 错误 60 : SSL certificate problem: unable to get local issuer certificate