简体   繁体   中英

Laravel Relationship in a pivot model

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM