努力定义包括属于属于的多态关系

发布于 2025-01-27 01:34:58 字数 1654 浏览 0 评论 0原文

我正在研究基于文本的基于RNG的Motorsport“模拟器”,现在我要添加资格。由于在现实生活中有许多不同类型的合格会议,因此我想让用户能够使用他们喜欢的任何格式。

我有一个季节模型,合格格式将属于该模型。不同的格式定义为单独的模型本身,例如ThreesessionEliminationSinglesession。这些迁移看起来像这样。

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_idfilesifing_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文