我有一张预订表,指向在room_bookings或restaurant_bookings表上预订的房间或餐厅桌面的迁移
售票:
Schema::create('bookings', function (Blueprint $table) {
$table->increments('id');
$table->morphs('booking');
$table->morphs('business');
$table->boolean('status');
}
room_bookings:
Schema::create('room_bookings', function (Blueprint $table) {
$table->increments('id');
$table->string('from');
$table->string('to');
}
restaurant_bookings:
Schema::create('restaurant_bookings', function (Blueprint $table) {
$table->increments('id');
$table->string('table_no');
}
餐厅:
Schema::create('restaurants', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
}
间:
Schema::create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('number_of_rooms');
}
在之前的项目中,我曾经用来定义与模型的单一关系。我想在桌子上保存预订ID,班级和房间/餐馆ID,班级和班级名称。
在我将单个类的实现定义为
之前 $boking = Booking::findOrFail($id);
//Defined relationsip on the model as booking()
$room->booking()->create($data)
模型如下
Business.php
class Booking extends Model
{
public function bookings()
{
return $this->morphTo();
}
public function business()
{
return $this->morphTo();
}
}
在Room.php和Restaurant.php上
public function booking()
{
return $this->morphMany(Booking::class, 'booking');
}
public function business()
{
return $this->morphMany(Booking::class, 'business');
}