Home > OS >  Laravel save tickets with changing numbers into database using a controller
Laravel save tickets with changing numbers into database using a controller

Time:03-17

Issue:
I need to put the amount selected using the <select> option into the database. But each of the <select> options have a possible different "amount".

So you have a UI like this:
Description - Price 50€ - Amount 2
Another Description - Price 20€ - Amount 2

If you click on checkout it looks like this: (screenshot created using dd($request))

enter image description here

The issue here is that with every new event this ticket_x changes, so you can have an event with ticket_1 and a different one with ticket_22 or ticket_75 etc.

What needs to be done?
The amount needs to be calculated (added) from all tickets.

So if you have these tickets:

ticket_5 with an amount of 3 and ticket_8 with an amount of 5 the final amount saved into the database would need to be 8

my CheckoutController.php:

    // Store data in database
    $checkout = new Checkout();
    $checkout->course_id = $request->course_id;

    // amount is currently hardcoded
    $checkout->amount = '2';
    // todo: grab the correct amount of the tickets as the tickets get renamed with each new event!
    //$checkout->amount = $request->ticket_1;
    $checkout->firstname = $request->firstname;
    $checkout->lastname = $request->lastname;
    $checkout->email = $request->email;
    $checkout->phone = $request->phone;
    $checkout->censored = $request->censored;
    $checkout->save();

frontend code:

    <select name="{{ 'ticket_' . $ticket->id }}"  data-name="{{ $ticket->title }}" data-id="{{ $ticket->id }}" data-price="{{ $ticket->price }}" size="1" x-model="ticket" x-on:change="$store.tickets.add_ticket({{ $ticket->id }}, ticket)" @if ($event->bookable == '1') disabled @endif>
       <option value="0" >0</option>
            @for ($i = $ticket->min; $i <= $ticket->max; $i  )
           <option value="{{ $i }}" >{{ $i }}</option>
         @endfor
    </select>

how the front-end looks:

enter image description here

CodePudding user response:

You can use <select name="tickets[]">...</select>

And after amount can be sum of array values.

  • Related