What is the best way to structure this context and show for each selected tab type the custom fields associated with this type of ticket?

I am trying to create a context like in the image.

enter image description here

So, for the "screen 1" of the image, I have the file "single.blade.php" associated with this route:

// Route for congress details page:
Route::get('/congress/{id}/{slug?}', [
    'uses' => 'FrontController@show',
    'as'   =>'congresses.show'
]);

So, when the user reaches " http: //project.test/congress/1/congress-title-test/ , he goes to the congress information page. On this page, in addition to congress details, there is a form in which the user selects the quantity, which he wants for each type of ticket.

This form has this selection menu with this name:

 <select name="types[{{ $type->name }}]">

An has this action and route:

// form action

<form method="post" action="{{route('congresses.storeQuantities', ['id' => $congress->id, 'slug' => $congress->slug])}}">

// Route form:

Route::post('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@storeQuantity',
    'as'   =>'congresses.storeQuantities'
]);

"", . , registration.blade.php. .

2 .

:

  • " " , , , .
  • , " ".
  • "collect_info_from_all_participants" "1", , , . . , " " "", "" - . , , .

, "" , , RegistrationController storeQuantity(), :

  • , "collect_info_from_all_participants" "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]);
}

}

// RegistrationPage

Route::get('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@displayRegistrationPage',
    'as'   =>'congresses.registration'
]);

register.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>

:

  • , . , ;
  • , , , ?
  • storeQuantity , . , ? - , , .
  • . - , , . - . - . , , , , , , . Im , .

, :

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');
    }
}
+4
1
  • Q: , . , ;

public function storeQuantity(Request $request, $id){
    ...
    $customQuestionsOfTicketType = $type->questions;
    ...
}

customQuestionsOfTicketType . $selectedTpyes[$type->name], :

$selectedTpyes[$type->name]['questions'] = $type->questions;

$selectedTpyes[$type->name] $customQuestions.


  • Q: Im , , , ?

(allParticipants) (selectedTypes), , , , . $customQuestionsOfTicketType.


  • Q: storeQuantity , , POST. , ? -, , .

, , , , .

.

. , :

Laravel, :


  • Q: . - , , . - . - . , , , , , , . Im , .

, . , , /step1 , /step2 1 .....

+2

Source: https://habr.com/ru/post/1696038/


All Articles