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

我在下面有一个注册表,用于在国会中注册用户.注册有4种不同的方案.因此,注册表需要处理这4种不同的场景.但它不起作用,只有方案1工作正常.

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

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

>国会“票类1”和“票类2”有两种票类型
>故障单类型“tt1”有一个自定义问题关联“你的手机是什么?”,故障单类型“tt2”没有任何关联的自定义问题
>所有参与者都是“1”,这意味着必须收集每个参与者的信息(姓名和姓氏)
>此方案的图表:
《php – 如何正确存储必要的信息以注册用户和大会中的其他参与者? (方案1有效,但方案2和3不适用)》

场景2:

>国会“tt1”和“tt2”有两种票类型
>票证类型“tt1”和“tt2”没有关联的任何自定义问题
>所有参与者都是“1”,这意味着必须收集每个参与者的信息
>此方案的图表:

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

场景3:

>国会“tt1”和“tt2”有两种票类型
>故障单类型“tt1”有一个自定义问题关联“你的手机是什么?”,故障单类型“tt2”没有任何关联的自定义问题
>所有参与者都是“0”,这意味着没有必要收集每个参与者的信息(姓名和姓氏)
>此方案的图表:
《php – 如何正确存储必要的信息以注册用户和大会中的其他参与者? (方案1有效,但方案2和3不适用)》

场景4:

>国会“tt1”和“tt2”有两种票类型
>票证类型“tt1”和“tt2”没有关联的任何自定义问题
>所有参与者都是“0”,这意味着没有必要收集每个参与者的信息(姓名和姓氏)
>此方案的图表:
《php – 如何正确存储必要的信息以注册用户和大会中的其他参与者? (方案1有效,但方案2和3不适用)》

上一页中选定的故障单可在变量“$selectedTypes”中找到

//在registration.blade.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>

// registration.blade.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');
    }
}

图中显示了我现在拥有的代码的一些问题,即问题中的代码:

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

最佳答案 我看到两件事

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);
}

希望它有效!
问候

点赞