Laravel Relationship in a pivot model

ε祈祈猫儿з 提交于 2020-01-07 02:35:21

问题


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:

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:


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


来源:https://stackoverflow.com/questions/41975152/laravel-relationship-in-a-pivot-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!