【发布时间】:2021-06-13 23:23:19
【问题描述】:
我不确定它的名字是否是反向关系,但是
我的事件模型包含:
public function venue()
{
return $this->hasOne('App\Models\Venue', 'id','venue_id');
}
所以基本上我可以使用事件模型访问场地数据,但我也想访问特定场地的所有事件数据。我知道我可以通过查询等来做到这一点,但是否可以通过关系做到这一点。
事件迁移
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->unsignedBigInteger('venue_id');
$table->decimal('price');
$table->text('content');
$table->timestamps();
$table->foreign('venue_id')->references('id')->on('venues');
场地迁移
$table->id();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->string('address');
$table->text('about');
$table->timestamps();
事件模型
public function venue()
{
return $this->hasOne('App\Models\Venue', 'id','venue_id');
}
场地模型
public function events()
{
return $this->belongsTo(Event::class, 'venue_id','id');
}
【问题讨论】: