本文介绍了数据透视模型中的Laravel关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表,其中之一是数据透视表(和数据透视模型),并尝试在数据透视模型(数据透视表中的外键)中创建belongsTo关系,以便我可以从其他一些表(具有主表)中获取相关名称钥匙).我想做的是通过以下图像进行说明:
数据透视表为:
其他表是:

I have three tables, one of them is pivot table (and pivot model) and trying to create belongsTo relationship in pivot model (foreign key in pivot table) so that I can get the relevant name from some other table(has primary key). What I want to do is illustrating by images below:
Pivot Table is:
And other table is:

它是枢轴模型:

class MproductIngredient extends Model {

public function qtyType() {
    return $this->belongsTo('App\TIngredientType','priQuantityTypeNo');
}

}
如何从其他表(具有主键)获取相关名称.

}
How to get the relevant name from other table(has primary key).

我的代码是:

@foreach($prd->ingredients a $ingredient)
     "{!! $ingredient->pivot->priQuantityTypeNo !!}"
     @endforeach

推荐答案

请详细说明我所理解的与您之间的关系

Please describe more as far i understand you can make below to relationships

    public function qtyTypes()
    {
        return $this->belongsToMany('App\TIngredientType', 'pivot_table_name',
          'main_table_id', 'TIngredientType_id');
    }
public function qtyType()
    {
        return $this->hasOne('App\TIngredientType');
    }

这篇关于数据透视模型中的Laravel关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 09:29