【发布时间】:2019-07-18 15:10:11
【问题描述】:
我是这里和 Laravel 的新手,请见谅。我有一个名为“产品”的表,该表通过多对一关系与“食谱”表相关(其中一个食谱有很多产品)。 -“食谱”表保留参考代码-这是我坚持的地方; 'recipes' 表与保存“真实”产品配方的三个不同表具有一对一的关系。这些表有不同的食谱内容,例如,
碱性表;
Schema::create('alkalines', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_bicarbonate');
$table->timestamps();
});
Acets 表;
Schema::create('acets', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('recipe_id');
$table->integer('sodium_chloride');
$table->integer('acetic_acid');
$table->timestamps();
});
如果我从其中之一开始(例如使用 Acet 模型),我能够获取所有关系。 但是如果我列出所有产品并尝试获取它的配方,我必须使用一堆“if 和 else”。就是买不到这样的食谱;
$product->recipe-> "one of the three recipe tables' content"
还有我的“食谱”表:
Schema::create('recipes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ref');
$table->timestamps();
});
我相信这很容易,只是缺少一些东西。请帮忙!提前致谢!
【问题讨论】:
标签: laravel laravel-5.8