努力定义包括属于属于的多态关系
我正在研究基于文本的基于RNG的Motorsport“模拟器”,现在我要添加资格。由于在现实生活中有许多不同类型的合格会议,因此我想让用户能够使用他们喜欢的任何格式。
我有一个季节
模型,合格格式将属于该模型。不同的格式定义为单独的模型本身,例如ThreesessionElimination
和Singlesession
。这些迁移看起来像这样。
Schema::create('three_session_eliminations', function (Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->foreignId('season_id')->constrained();
$table->unsignedInteger('q2_driver_count');
$table->unsignedInteger('q3_driver_count');
$table->unsignedInteger('runs_per_session');
$table->unsignedInteger('min_rng');
$table->unsignedInteger('max_rng');
$table->timestamps();
});
我最初的想法是
Schema::create('single_sessions', function (Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->foreignId('season_id')->constrained();
$table->unsignedInteger('runs_per_session');
$table->unsignedInteger('min_rng');
$table->unsignedInteger('max_rng');
$table->timestamps();
});
将每个合格格式模型添加
public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
到每个合格格式模型中,但显然不能是hasone
,因为每种不同格式都有不同的表。
我看过“一个”对于许多(多态)的“关系文档,但我无法完全围绕在我的情况下如何应用它。我是否必须将fimefify_format_id
和filesifing_format_type
添加到我的season
型号中,然后从每个格式迁移中删除season_id
列做这项工作?
I'm working on a text-based, rng-based motorsport "simulator" and I'm now at the point where I want to add qualifying. Since there's many different types of qualifying sessions across real life motorsport, I want to give the user the ability to use whatever format they like.
I have a Season
model, to which the a qualifying format will belong. The different formats are defined as separate models themselves, for example ThreeSessionElimination
and SingleSession
. The migrations for these would look something like this;
Schema::create('three_session_eliminations', function (Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->foreignId('season_id')->constrained();
$table->unsignedInteger('q2_driver_count');
$table->unsignedInteger('q3_driver_count');
$table->unsignedInteger('runs_per_session');
$table->unsignedInteger('min_rng');
$table->unsignedInteger('max_rng');
$table->timestamps();
});
and
Schema::create('single_sessions', function (Blueprint $table) {
$table->unsignedBigInteger('id')->primary();
$table->foreignId('season_id')->constrained();
$table->unsignedInteger('runs_per_session');
$table->unsignedInteger('min_rng');
$table->unsignedInteger('max_rng');
$table->timestamps();
});
My initial thought was to add
public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}
to each qualifying format model, but obviously the inverse can't be a HasOne
since there's different tables for each different format.
I've had a look at the "One To Many (Polymorphic)" relation documentation, but I can't quite wrap my head around how I should apply that in my case. Would I have to add a qualifying_format_id
and qualifying_format_type
to my Season
model and remove the season_id
column from each format migration to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论