I'm not sure how to best structure my tables so that the storage and querying is efficient and easy..
This is what i have:
- rooms table - containing 10+ different room types (Kitchen, Lounge, etc), (with a unique ID)
- properties_rooms lookup table - the Properties ID and the Room_id
- properties table - containing many different properties (with a unique ID)
In addition to the above, i need to be able to capture a specific note and level attribute for each Property/Room combination, for example:
- (Property ID - '1') -> from properties table
- (Property Name -'Sunny House')-> from properties table
- (Room ID - '2')-> from properties_rooms lookup
- (Room Name - 'Bedroom')-> from Rooms table
- (Level - "Ground Floor")-> what table??
- (Note - "Twin beds")-> what table??
Level and Note above are specific to the combination of the property and the room type, so seem to make sense to be in the lookup?
I have the following scheme setup:
Properties
Schema::create('properties', function (Blueprint $table) {
$table->id();
.....
Rooms and Properties_Rooms
Schema::create('rooms', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->timestamps();
});
Schema::create('properties_room', function (Blueprint $table) {
$table->id();
$table->foreignId('properties_id')->constrained('properties')->onDelete('cascade');
$table->foreignId('room_id')->constrained('rooms')->onDelete('cascade');
$table->string('level');
$table->string('note');
$table->timestamps();
$table->unique(['properties_id','room_id']);
});
Models
Rooms
public function properties()
{
return $this->belongsToMany(Properties::class);
}
Properties
public function rooms()
{
return $this->belongsToMany(Room::class);
}
The "Level" and "Note" fields seem correct to be in the lookup file. But i'm not sure if this is correct, or how to access them?
In my blade view, these all work:
- {{ $properties }} - Property Table Attributes
- {{ $properties->rooms }} - Room Table Attributes
But through the "Room" attributes, i'm not able to see the information held in the lookup file, which was obviously used?