0
votes

I have a single.blade.php file that shows a congress details and has also a form so the user can select the quantity of tickets that he wants for each ticket type.

When user selects the quantities and click next the request goes to the RegistrationController that has the storeQuantity() method to stores the selected quantities by the user in the array $selectedTypes and redirects the user to the registration.blade.php with the $selectedTypes array:

class RegistrationController extends Controller
{
     public function storeQuantityandRedirect(Request $request, $id){

            $typeQuantities = $request->get('types');

            foreach($typeQuantities as $typeName => $quantity){
                $type = TicketType::where('name', $typeName)->firstOrFail();
                $price = $type->price;
                $selectedTypes[$type->name]['quantity'] = $quantity;
                $selectedTypes[$type->name]['price'] = $price;

            }
            return view('congresses.registration')->with('selectedTypes', $selectedTypes);
        }
}

Now in the registration.blade.php file, the user needs to fill in the registration form.

If the user selected, for example, 2 tickets in the previous page in the registration page should appear two sections for the user introduce the name and email for each ticket type. This is working fine with the code below:

@foreach($selectedRtypes as $k=>$selectedRtype)
    <h6>Participant - 1 - Ticket Type - {{$k}}</h6> <!-- The variable {{$k}} shows the name of each ticket type (ex: full access, basic, etc). -->
    <div class="form-group font-size-sm">
        <label for="name" class="text-gray">Name</label>
        <input type="text" class="form-control" id="name" value="">
    </div>
    <div class="form-group font-size-sm">
        <label for="surname" class="text-gray">Surname</label>
        <input type="text" class="form-control" name="surname" id="surname" value="">
    </div>
@endforeach

Doubt:

Each ticket type can have custom questions.

My doubt is how to show besides the name and surname fields show also the custom fields of each specific ticket type. For example, the congress organizer may have created for the ticket type "full access" the custom question "What is your email?". So in the foreach when the ticket type is "full access" it should appear, besides the name and surname, the field "what is your email?".

Do you know how to do that?

I have this table content and relationships:

questions table:
id      question                congress_id
1       whats your email             1

ticket_type_questions table:
id        ticket_type_id      question_id
1            1                     1      (the ticket type 1 has the question 1)

ticket types table
id             name        congress_id
1             full access        1
2             basic               1
1

1 Answers

1
votes

Since you have the questions stored in a database table, you can pass them to your view from your controller. In your controller, you can add something like this:

$customQuestions = Question::where('congress_id', $cong_id); 

Then modify your return to look something like this.

return response()->view('congress.registration', ['selectedTypes' => $selectedTypes, 'customQuestions' => $customQuestions], 200);

Then, on your view, right before the @endforeach, you can add a conditional to check if there are any custom questions.

// put this right before your @endforeach
@if(count($custQuestions) > 0)
// loop and display custom question fields.
@endif

Hope this helps.