Php 3个表的拉维关系

Php 3个表的拉维关系,php,laravel,eloquent,relationship,Php,Laravel,Eloquent,Relationship,我在一家Laravel的后台工作,主要目标是与餐馆(商店)的接受者打交道,并计算每天、商店、轮班提供的菜肴数量 我需要详细说明一份报告,显示一位受访者的排名,获得受访者的姓名和上菜的数量(从记录表中) 这里有相关的表格: table recipees - id - name - description table day_recipee_shop_workshifts - id - day_id - recipee_id - shop_id - workshift_id t

我在一家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
我要做的是从records表中获取按计数排序的所有记录,并为每个记录获取收件人的名称

我试过处理拉威尔的关系,但只有一次,那就是运气不佳。也许我误解了如何实现它,但我认为我可能能够通过day_recipee_shop_workshift表从记录模型中检索recipee的名称


提前感谢您的评论

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

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' 
            );
        }

已编辑检查它是否有效

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

Record::with(['day_recipee_shop_workshifts.recipees'])->orderBy('count','desc')->get();
其中,day\u Recipeer\u shop\u workshifts是在记录中定义的关系名称,recipees是在day RecipeeShop workshifts模型中定义的关系名称

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

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

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


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

尝试了,但出现错误:SQLSTATE[42S22]:未找到列:1054未知列“字段列表”中的“day\u recipee\u shop\u workshift.day\u recipee\u shop\u workshift\u id”(SQL:select
recipees
*,
day\u recipees\u shop\u workshift
day\u recipees\u shop\u workshift\u id
as
laravel\u通过
键从
recipees
内部连接
day\u recipees\u shop\u workshift
在day\u recipees\u shop\u workshift上e
day\u recipee\u shop\u workshift
deleted\u at
为空,
day\u recipee\u shop\u workshift
day\u recipee\u workshift\u id
deleted\u at
为空限制1)试图从表中获取错误列检查现在是否正常@Hmorvso怪异…它失败了,这是应用编辑后生成的查询:选择
recipees
*,
day\u recipees\u shop\u工作班次
recipees\u id
as
larvel\u通过
recipees
内部连接
day\u recipees\u shop\u工作班次
日受访人(店)工作班次
日受访人
日受访人(店)工作班次在处删除的(店)为空日受访人(店)工作班次
日受访人(店)id
受访人
处删除的(为空我尝试过的限制为1现在有很多组合调用hasOneThrough和no luckMaybe?天哪,你的表名是looong@HmorvYeah,查询似乎构建得很好…抱歉,我无法进一步帮助你…但我认为你接近完成它了!你能进一步开发它吗?在同一个模型上有3个函数?是的,你需要创建函数假设函数A(){relationwithhasmany}函数B(){relationwithhasmany}函数c(){relationwithhasmany}并在controllerok中执行您的逻辑,但我试图避免的只是在controller中添加逻辑(甚至在最终的存储库中).为什么它只能用关系来解决?它只是三个表之间的关系,这就是它让我疯狂的地方