【问题标题】:Laravel relationship with 3 tablesLaravel 与 3 个表的关系
【发布时间】:2020-06-12 12:24:46
【问题描述】:

我正在开发 Laravel 的后端,主要目标是处理与餐厅(商店)的食谱,并计算每天、商店、轮班供应的菜肴数量。

我需要详细说明一份报告,显示食谱的排名,获取食谱的名称和提供的菜肴数量(来自记录表)。

你有涉及的表:

table recipees
 - id
 - name
 - description

table day_recipee_shop_workshifts

 - id
 - day_id
 - recipee_id
 - shop_id
 - workshift_id

table records
 - id
 - day_recipee_shop_workshift_id
 - count

我要做的是从按计数排序的记录表中​​获取所有记录,并为每条记录获取配方者的姓名。

我已经尝试过 Laravel 的关系,如 hasOneThrough、belongsToMany,但没有成功。也许我误解了如何实现它,但我想我可以通过 day_recipee_shop_workshift 表从记录模型中检索食谱的名字。

提前感谢您的 cmets!

【问题讨论】:

    标签: php laravel eloquent relationship


    【解决方案1】:

    hasOneThrough 是正确的方法,要实现这一点,请将此关系放入您的 Record 模型中:

    public function recipee()
            {
                return $this->hasOneThrough(
                    'App\Recipee', //Check if this is correct model name
                    'App\DayRecipeeShopWorkShifts', //Check if this is correct model name
                    'recipee_id', 
                    'id', 
                    'day_recipee_shop_workshift_id', 
                    'recipee_id' 
                );
            }
    

    已编辑检查它是否有效!

    【讨论】:

    • 试过但出错:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'day_recipee_shop_workshifts.day_recipee_shop_workshift_id' in 'field list' (SQL: select recipees.*, day_recipee_shop_workshifts. day_recipee_shop_workshift_id as laravel_through_key from recipees inner join day_recipee_shop_workshifts on day_recipee_shop_workshifts.id = recipees.recipee_id 其中day_recipee_shop_workshifts.@9876545334@.@9876545334@.day_recipee_shop_workshift_id为空= 11 和 recipees.deleted_at 为空限制 1) 试图从表中获取错误的列
    • 检查现在是否正常@Hmorv
    • 太奇怪了......它失败了,这是它在应用编辑后生成的查询:select recipees.*, day_recipee_shop_workshifts.recipee_id as laravel_through_key from recipees inner在day_recipee_shop_workshifts.id 上加入day_recipee_shop_workshifts = recipees.id 其中day_recipee_shop_workshifts.deleted_at 为空,day_recipee_shop_workshifts.recipee_id = ?和 recipees.deleted_at 是空限制 1 我尝试了很多组合调用 hasOneThrough 并且没有运气
    • 现在可以吗?耶稣,你的表名很长@Hmorv
    • 是的,查询似乎构建得很好......对不起,我无法进一步帮助你......但我认为你即将完成它!
    【解决方案2】:

    您可以通过以下查询获取记录:

    Record::with(['day_recipee_shop_workshifts.recipees'])->orderBy('count','desc')->get();
    

    其中 day_recipee_shop_workshifts 是在 Record 模型中定义的关系名称,recipees 是在 DayRecipeeShopWorkShifts 模型中定义的关系名称。

    现在您可以获得如下配方名称:

    $recipeName = $record->day_recipee_shop_workshifts->recipees->name;
    

    【讨论】:

      【解决方案3】:

      在模型中创建三个函数 写每个函数都有很多关系,id是外键

      $this->hasMany(\App\ModelName::class, 'post_id', 'id');

      【讨论】:

      • 你能进一步开发它吗?同一型号有 3 个功能?
      • 是的,您需要创建函数,假设函数 A(){ 与 hasMany 的关系 } 函数 B(){与 hasMany 的关系 } 函数 c(){与 hasMany 的关系 } 并在控制器中执行您的逻辑跨度>
      • 好的,但我试图避免的只是在控制器中添加逻辑(即使在最终的存储库中)。为什么只能使用关系来解决?这只是 3 个表之间的关系,这就是让我抓狂的地方
      猜你喜欢
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多