gpt4 book ai didi

php - Laravel 枢轴模型中的关系

转载 作者:行者123 更新时间:2023-11-29 19:28:29 24 4
gpt4 key购买 nike

我有三个表,其中一个是数据透视表(和数据透视模型),并尝试在数据透视模型中创建属于关系(数据透视表中的外键),以便我可以从其他表(具有主表)获取相关名称 key )。我想做的是用下面的图片来说明:
数据透视表是:
enter image description here其他表是:
enter image description here

它是枢轴模型:

class MproductIngredient extends Model {

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

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

我的代码是:

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

最佳答案

请描述更多据我所知您可以在下面描述的关系

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

关于php - Laravel 枢轴模型中的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com