Модель отеля
Код: Выделить всё
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;
}
Код: Выделить всё
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()]);
}
Код: Выделить всё
{{ $hotel->name }}
{{ $hotel->description }}
Rooms
@csrf
Select Room Type:
Select Type
@foreach($hotel->rooms as $room)
@if ($room->roomTypes)
@foreach($room->roomTypes as $roomType)
{{ $roomType->name }} - ${{ $roomType->price }}
@endforeach
@else
No Room Types Available
@endif
@endforeach
Quantity:
Price per Room Type: $0.00
Total Price: $0.00
Book Now
Room Images
@foreach($hotel->rooms as $room)
[h4]{{ $room->name }}[/h4]
@foreach($room->images as $image)
[img]{{ asset([/img]
image_path) }}" alt="Room Image" width="100">
@endforeach
@endforeach

Подробнее здесь: https://stackoverflow.com/questions/792 ... y-as-blank