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 for a trip

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
1 reply
davy_yg's avatar

If you want to filter the seat based on the date, you probably needs to filter the query builder as well:

For example:

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

Please or to participate in this conversation.