Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nae's avatar
Level 1

insert in database number of seats

How to insert in database number of the seat from the bus and check what seats are available. What I have now. First I create a bus at table buses, I insert a name for the bus and number of seats(example 50). When I create a trip I choose a bus by id and insert this in table bus_for_routes. This code in the controller make array for available seat numbers

$bus_info = DB::table('bus_for_routes')->get();

$booking_info = DB::table('booking_nows')->where('route_id', $id)->get();

    $busy_seat_array = [];
    if ( count($booking_info) > 0 ) {
        foreach ( $booking_info as $value ) {
            array_push($busy_seat_array, $value->seat_no);
        }
    }

    return view....

This code in View

                    @php  
                    $bus_total_seat = $bus_info[0]->bus_id;
                    for ( $i = 1; $i <= $bus_total_seat; $i++ ) {
                      if ( ! in_array($i, $busy_seats) ) {
                        
                           echo '<option   value=" '.$i.' ">' .$i . "" .'</option>';
                        
                      }
                    }
                
                  @endphp
                 
                  </select> 

I can select the number of seats and show only the available seats. How I do it is work, but I don't know if is good how I do, maybe exist another solution better, becouse i have a problem, i want to show the seats depends of what date(example 09/20/2019 you choose for booking. When i create a trips i have for one trip many date for this trip and for this dates i choose the bus, In the view it show all dates, but show seats only for first date.

0 likes
11 replies
nae's avatar
Level 1

I want to find more ways how to insert the number of seats, but know i just insert for example 50 $bus = new Bus(); $bus->name=$request->name; $bus->seats=$request->seats;

jlrdw's avatar

Actually there's examples in the documentation, of insert, create, save etc.

nae's avatar
Level 1

I know how to insert, but I need to know how to do correct for the number of seats for bus, how to check many seats are available and how much not. This is my problem

tykus's avatar

Understand that we know nothing about your app @nae - if you re-read your OP, how can you expect anyone to understand what you need; what you have already done; what is not working...

nae's avatar
Level 1

i update the Original Post, maybe know posting is clearer or not, sorry for my English, thanks)))

jlrdw's avatar

Just trying to help when I say, if not using blade at least template with php instead of echo.

<td class="comment"><?php echo $row->comments; ?></td>
// or short tag
<?=

But what are you trying to do after the select.

nae's avatar
Level 1

the select is in form for booking a ticket for trip with bus

jlrdw's avatar

Well you need this in a form and post the form and go from there.

Jeffrey has a free video from scratch series. But it's in English, if you could find someone to view it with you and explain he teaches this basic stuff real good.

nae's avatar
Level 1

can send the link for this videos?

Please or to participate in this conversation.