代码之家  ›  专栏  ›  技术社区  ›  user9659025

如何正确存储必要的信息以注册用户和大会的其他参与者?(方案1有效,但方案2和3无效)

  •  4
  • user9659025  · 技术社区  · 8 年前

    我在下面有一份在国会注册用户的登记表。注册有4种不同的场景。因此,注册表需要处理这4种不同的情况。但它不工作,只有场景1工作正常。

    您知道如何实现这种情况吗?

    场景1:(唯一正常工作的场景)

    • 大会有两种门票类型“门票类型1”和“门票类型2”
    • 票证类型“tt1”有一个自定义问题关联“What at your phone?”,票证类型“tt2”没有任何相关的自定义问题
    • 所有参与者均为“1”,这意味着需要收集每个参与者的信息(姓名)
    • 此场景的示意图: enter image description here

    场景2:

    • 大会有两种门票类型“tt1”和“tt2”
    • 票证类型“tt1”和“tt2”没有任何相关的自定义问题
    • 所有参与者均为“1”,这意味着需要收集每个参与者的信息
    • 此场景的示意图:

    enter image description here

    场景3:

    • 大会有两种门票类型“tt1”和“tt2”
    • 票证类型“tt1”有一个自定义问题关联“What at your phone?”,票证类型“tt2”没有任何相关的自定义问题
    • 所有参与者均为“0”,这意味着无需收集每个参与者的信息(姓名)
    • 此场景的示意图: enter image description here

    场景4:

    • 大会有两种门票类型“tt1”和“tt2”
    • 票证类型“tt1”和“tt2”没有任何相关的自定义问题
    • 所有参与者均为“0”,这意味着无需收集每个参与者的信息(姓名)
    • 此场景的示意图: enter image description here

    从上一页中选择的票证在变量“”中可用 $selectedTypes “”

    //注册表中的注册表。刀身php页面

          <form method="post" id="step1form" action="">
        {{csrf_field()}}
        @if (!empty($allParticipants))
            @if($allParticipants == 1)
                <p>Please fill in all fields. Your tickets will be sent to
                    p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
    
                @foreach($selectedTypes as $k => $selectedType)
                    @foreach(range(1,$selectedType['quantity']) as $val)
    
                        <h6>Participant - {{$val}}  - {{$k}}</h6>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="payment_method" value="referencias">
                            <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                                <span class="mr-auto">Fill the following fields with the authenticated user information.</span>
                            </label>
                        </div>
                        <div class="form-group font-size-sm">
                            <label for="participant_name" class="text-gray">Name</label>
                            <input type="text" name="participant_name[]" required class="form-control" value="">
                        </div>
                        <div class="form-group font-size-sm">
                            <label for="participant_surname" class="text-gray">Surname</label>
                            <input type="text" required class="form-control" name="participant_surname[]" value="">
                        </div>
                        <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>
                        @foreach($selectedType['questions'] as $customQuestion)
                            <div class="form-group">
                                <label for="participant_question">{{$customQuestion->question}}</label>
                                <input type="text"
                                       @if($customQuestion->pivot->required == "1") required @endif
                                       class="form-control" name="participant_question[]">
                                <input type="hidden" name="participant_question_required[]"
                                       value="{{ $customQuestion->pivot->required }}">
                                <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                            </div>
                        @endforeach
                    @endforeach
                @endforeach
                @else
                    <p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
    
                @endif
            @endif
    
        <input type="submit" href="#step2"
               id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
    </form>
    

    //注册。刀身php在大会中注册用户的完整方法

    public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
        $allParticipants = Congress::where('id', $id)->first()->all_participants;
        $user = Auth::user();
    
        if($allParticipants){
            $rules = [
                'participant_name.*' => 'required|max:255|string',
                'participant_surname.*' => 'required|max:255|string',
            ];
    
            $messages = [
                'participant_question.*.required' => 'The participant is required'
            ];
    
            foreach ($request->participant_question_required as $key => $value) {
                $rule = 'string|max:255'; // I think string should come before max
                //dd($value);
                // if this was required, ie 1, prepend "required|" to the rule
                if ($value) {
                    $rule = 'required|' . $rule;
                }
    
                // add the individual rule for this array key to the $rules array
                $rules["participant_question.{$key}"] = $rule;
            }
    
            $validator = Validator::make($request->all(), $rules, $messages);
    
            if($validator->passes()) {
                $registration = Registration::create([
                    'congress_id' => $id,
                    'main_participant_id' => $user->id,
                    'status' => 'C',
                ]);
    
                $participants = [];
    
                for ($i = 0; $i < count($request->participant_name); $i++)
                    $participants[] = Participant::create([
                        'name' => $request->participant_name[$i],
                        'surname' => $request->participant_surname[$i],
                        'registration_id' => $registration->id,
                        'ticket_type_id' => $request->rtypes[$i]
    
                    ]);
    
                for ($i = 0; $i < count($request->participant_question); $i++)
                    $answer = Answer::create([
                        'question_id' => $request->participant_question_id[$i],
                        'participant_id' => $participants[$i]->id,
                        'answer' => $request->participant_question[$i],
                    ]);
                }
    
            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
        }
    
    
        else {
    
            $messages = [
                'participant_question.*.required' => 'The participant is required'
            ];
    
    
            foreach ($request->participant_question_required as $key => $value) {
                $rule = 'string|max:255'; // I think string should come before max
                //dd($value);
                // if this was required, ie 1, prepend "required|" to the rule
                if ($value) {
                    $rule = 'required|' . $rule;
                }
    
                // add the individual rule for this array key to the $rules array
                $rules["participant_question.{$key}"] = $rule;
            }
    
    
            $validator = Validator::make($request->all(), $rules, $messages);
    
    
            if ($validator->passes()) {
    
                $registration = Registration::create([
                    'congress_id' => $id,
                    'main_participant_id' => $user->id,
                    'status' => 'C',
    
                ]);
    
                $participants = [];
    
                for ($i = 0; $i < count($request->participant_name); $i++)
                    $participants[] = Participant::create([
                        'name' => '',
                        'surname' => '',
                        'registration_id' => $registration->id,
                        'ticket_type_id' => $request->rtypes[$i]
    
                    ]);
    
                for ($i = 0; $i < count($request->participant_question); $i++)
                    $answer = Answer::create([
                        'question_id' => $request->participant_question_id[$i],
                        'participant_id' => $participants[$i]->id,
                        'answer' => $request->participant_question[$i],
                    ]);
            }
    
            return response()->json([
                'success' => true,
                'message' => 'success'
            ], 200);
    
        }
    }
    

    问题相关模型:

    class Congress extends Model
    {
        // A conference has many ticket types
        public function ticketTypes(){
            return $this->hasMany('App\TicketType', 'congress_id');
        }
    
        public function registrations(){
            return $this->hasMany('App\Registration', 'congress_id');
        }
    }
    
    // RegistrationModel
    class Registration extends Model
    {
        // a registration has one user that do the registration (main_participant_id)
        public function customer(){
            return $this->belongsTo('App\User');
        }
        public function congress(){
            return $this->belongsTo('App\Congress');
        }
    }
    
    class TicketType extends Model
    {
        public function congress(){
            return $this->belongsTo('App\Congress');
        }
    }
    
    class Question extends Model
    {
        public function registration_type(){
            return $this->belongsToMany('App\RegistrationType', 'ticket_type_questions')
                ->withPivot('required');
        }
    }
    
    class Answer extends Model
    {
        public function question(){
            return $this->belongsTo('Question');
        }
        public function participant(){
            return $this->belongsTo('Participant');
        }
    }
    

    图中显示了我目前遇到的一些代码问题,这就是问题中的代码:

    enter image description here

    3 回复  |  直到 8 年前
        1
  •  1
  •   00Riser    8 年前

    我看到两件事

    1.-当$allParticipants==0时,表单不会发送任何内容,可能更像:

        <form method="post" id="step1form" action="">
        {{csrf_field()}}
        @if (!is_null($allParticipants) && is_int($allParticipants))
            @if($allParticipants == 1)   
                <p>Please fill in all fields. Your tickets will be sent to
                    p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
            @else
                <p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
            @endif
    
            @foreach($selectedTypes as $selectedType)
                @foreach(range(1,$selectedType['quantity']) as $test)
                    <h6>Participant - 1 - {{$test}}</h6>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="payment_method" value="referencias">
                        <label class="form-check-label d-flex align-items-center" for="exampleRadios1">
                            <span class="mr-auto">Fill the following fields with the authenticated user information.</span>
                        </label>
                    </div>
                    @if($allParticipants == 1)
                        <div class="form-group font-size-sm">
                            <label for="participant_name" class="text-gray">Name</label>
                            <input type="text" name="participant_name[]" required class="form-control" value="">
                        </div>
                        <div class="form-group font-size-sm">
                            <label for="participant_surname" class="text-gray">Surname</label>
                            <input type="text" required class="form-control" name="participant_surname[]" value="">
                        </div>
                       @foreach($selectedType['questions'] as $customQuestion)
                        <div class="form-group">
                            <label for="participant_question">{{$customQuestion->question}}</label>
                            <input type="text"
                                    @if($customQuestion->pivot->required == "1") required @endif
                                    class="form-control" name="participant_question[]">
                            <input type="hidden" name="participant_question_required[]"
                                    value="{{ $customQuestion->pivot->required }}">
                            <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                        </div>
                       @endforeach
                    @else
                        <input type="hidden" value="foo" name="participant_name[]"/>
                        <input type="hidden" value="bar" name="participant_surname[]"/>
                    @endif
                    <input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>                
                @endforeach
                @if ($allParticipants == 0) 
                  @foreach($selectedType['questions'] as $customQuestion)
                        <div class="form-group">
                            <label for="participant_question">{{$customQuestion->question}}</label>
                            <input type="text"
                                    @if($customQuestion->pivot->required == "1") required @endif
                                    class="form-control" name="participant_question[]">
                            <input type="hidden" name="participant_question_required[]"
                                    value="{{ $customQuestion->pivot->required }}">
                            <input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
                        </div>
                    @endforeach
                @endif
            @endforeach
        @endif
    
        <input type="submit" href="#step2"
                id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
        </form>
    

    2.-问题似乎取决于门票类型,是可选的,功能应该考虑到这一点。

    public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
        $allParticipants = Congress::where('id', $id)->first()->all_participants;
        $user = Auth::user();
    
        $rules = [];
        $messages = [];
    
        if(isset($request->participant_question_required)) {
            $messages = [
                'participant_question.*.required' => 'The participant is required'
            ];
    
            foreach ($request->participant_question_required as $key => $value) {
                $rule = 'string|max:255'; // I think string should come before max
                //dd($value);
                // if this was required, ie 1, prepend "required|" to the rule
                if ($value) {
                    $rule = 'required|' . $rule;
                }
    
                // add the individual rule for this array key to the $rules array
                $rules["participant_question.{$key}"] = $rule;
            }
        }
    
        if($allParticipants){
            $rules = [
                'participant_name.*' => 'required|max:255|string',
                'participant_surname.*' => 'required|max:255|string',
            ];
        }
    
        $validator = Validator::make($request->all(), $rules, $messages);
    
        if($validator->passes()) {
            $registration = Registration::create([
                'congress_id' => $id,
                'main_participant_id' => $user->id,
                'status' => 'C',
            ]);
    
            $participants = [];
            for ($i = 0; $i < count($request->participant_name); $i++) {
                $name = ($allParticipants) ? $request->participant_name[$i] : '';
                $surname = ($allParticipants) ? $request->participant_surname[$i] : '';
                $participants[] = Participant::create([
                    'name' => $name,
                    'surname' => $surname,
                    'registration_id' => $registration->id,
                    'ticket_type_id' => $request->rtypes[$i]
    
                ]);
            }
    
            if (isset($request->participant_question))
                for ($i = 0; $i < count($request->participant_question); $i++)
                        $answer = Answer::create([
                            'question_id' => $request->participant_question_id[$i],
                            'participant_id' => $participants[$i]->id,
                            'answer' => $request->participant_question[$i],
                        ]);
        }
    
        return response()->json([
            'success' => true,
            'message' => 'success'
        ], 200);
    }
    

    希望它能起作用! 当做

        2
  •  1
  •   Tom Headifen    8 年前

    如果希望向参与者存储多个ticket\u类型,则需要使用数据透视表。数据透视表是仅包含ID并以多对多方式将数据关联在一起的表。

    e、 g。

    participants_ticket_type table
    id      ticket_type_id   participant_id
    1          1               2
    2          2               2
    

    注意参与者2的ticket\u类型是如何为1和2的?这是一种多对多的关系。

    Laravel为您处理这些关系,您可以在他们的文档中找到。 https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

    通常,如果您发现自己在一列中存储了多个id,那么多对多关系将解决此问题。

        3
  •  -1
  •   jay dave    8 年前

    首先使用 select * from TABLENAME 然后使用 COUNT 函数在查询中或使用PHP Count laravel刀片文件上的函数 然后执行代码。。

    在刀片文件上

    @if(count($records)>0){
       // Your Code
    
    }
    
    推荐文章