房间类型淹没显示为空白 Laravel 11

问题描述 投票:0回答:1

我是 laravel 的初级程序员。当我在页面上显示酒店详细信息页面时,房间类型会被淹没显示为空白。 我通过 dd() 进行了测试;显示为空 我在下面附加了我尝试过的代码。有什么问题可以解决吗

房间模型

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;
}

房型型号

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;
}

房间图像模型

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);
    }

}

家庭控制器

 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

<!DOCTYPE html>
<html lang="en">
    <head>
       
    </head>
    <body>
<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



</body>
</html>
laravel laravel-11
1个回答
0
投票

房间模型中多种房间类型的函数名称是“roomType” 在视图文件中,您正在使用“roomTypes”检索它

© www.soinside.com 2019 - 2024. All rights reserved.