Search code examples
phplaravellaravel-11

Room types drop drown diplay as blank


I am a beginner Laravel programmer. When I display the hotel details page, the room types dropdown is displayed as blank. I tested it using dd() and it shows as null. Below is the code I tried. What is the issue? Can someone help me solve it?

Hotel model

class Hotel extends Model
{ 
    protected $table = 'hotels';
    protected $primaryKey = 'id';
    protected $fillable = ['name', 'image', 'description', 'status'];

    public function rooms()
    {
        return $this->hasMany(Room::class, 'hotel_id', 'id'); // Correctly references the rooms table
    }

    use HasFactory;
}

Room model

class Room extends Model
{
    protected $table = 'rooms';
    protected $primaryKey = 'id';
    protected $fillable = ['name', 'description', 'qty', 'hotel_id', 'status'];
    
    public function hotel()
    {
        return $this->belongsTo(Hotel::class, 'hotel_id', 'id'); // Assumes 'id' is the primary key of 'hotels'
    }
    
    public function images()
    {
        return $this->hasMany(RoomImage::class);
    }
    
    public function roomType()
    {
        return $this->hasMany(RoomType::class); // Ensure a room_type_id column exists
    }


    use HasFactory;
}

RoomType model

class RoomType extends Model
{
    protected $table = 'room_types';
    protected $primaryKey = 'id';
    protected $fillable = ['name', 'room_id', 'price'];
    
    public function room()
    {
        return $this->belongsTo(Room::class);
    }
    
    use HasFactory;
}

RoomImage Model

class RoomImage extends Model
{
    use HasFactory;

    protected $table = 'room_images';
    protected $primaryKey = 'id';
    protected $fillable = ['room_id', 'image_path'];

    public function rooms()
    {
        return $this->belongsTo(Room::class);
    }
}

HomeController

public function showdetails($id)
{
    $hotel = Hotel::with([
        'rooms.images',       // Fetch related images for each room
        'rooms.roomType'      // Fetch related room type for each room
    ])->where('id', $id)->get();
   
    if ($hotel->isEmpty()) {
        abort(404, 'Hotel not found.');
    }
  
    return view('showdetails', ['hotel' => $hotel->first()]);
}

showdetails.blade.php

<h1>{{ $hotel->name }}</h1>
<p>{{ $hotel->description }}</p>

<h2>Rooms</h2>
<form action="{{ route('booking.bookRoom') }}" method="POST">
  @csrf
  <div>
    <label for="roomType">Select Room Type:</label>
    <select id="roomType" name="room_id" onchange="updatePrice()">
      <option value="">Select Type</option>
      @foreach($hotel->rooms as $room)
        @if ($room->roomTypes)
          @foreach($room->roomTypes as $roomType)
            <option value="{{ $roomType->id }}" data-price="{{ $roomType->price }}">
              {{ $roomType->name }} - ${{ $roomType->price }}
            </option>
          @endforeach
        @else
          <option value="" disabled>No Room Types Available</option>
        @endif
      @endforeach
    </select>
  </div>

  <div>
    <label for="quantity">Quantity:</label>
    <input type="number" id="quantity" name="quantity" value="1" min="1" onchange="calculateTotalPrice()">
  </div>

  <div>
    <p>Price per Room Type: $<span id="pricePerRoom">0.00</span></p>
    <p>Total Price: $<span id="totalPrice">0.00</span></p>
  </div>

  <button type="submit" class="btn btn-success">Book Now</button>
</form>

<h3>Room Images</h3>
@foreach($hotel->rooms as $room)
  <h4>{{ $room->name }}</h4>
  <div>
    @foreach($room->images as $image)
      <img src="{{ asset('storage/' . $image->image_path) }}" alt="Room Image" width="100">
    @endforeach
  </div>
@endforeach

dd($hotel)


Solution

  • Your function name for multiple room types in rooms model is "roomType" and in view file, you are retrieving it using "roomTypes"

    Also $room->roomType returns a collection in all cases (even with no results) since it's a hasMany relation. to check of there are results, you need to call count()

    @if ($room->roomType->count())
        @foreach($room->roomType as $roomType)
            <option value="{{ $roomType->id }}" data-price="{{ $roomType->price }}">
                {{ $roomType->name }} - ${{ $roomType->price }}
            </option>
        @endforeach
    @else
        <option value="" disabled>No Room Types Available</option>
    @endif