dsfw2154 2017-02-01 08:35
浏览 31

枢轴模型中的Laravel关系

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:
enter image description here And other table is:
enter image description here

It is pivot Model:

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).

My code is:

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

1条回答 默认 最新

  • dongpan8439 2017-02-01 08:58
    关注

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

    for belongstoMany

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

    for hasOne

    public function qtyType()
        {
            return $this->hasOne('App\TIngredientType');
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?