1 回答

TA貢獻1831條經(jīng)驗 獲得超10個贊
創(chuàng)建貨幣模型并添加foreign_key:product_id
php artisan make:model Currency
在產(chǎn)品和貨幣之間構(gòu)建一對多,您需要在表中添加。product_idcurrencies
在您的貨幣模型中:
public function product() {
return $this->belongsTo('App\Product', 'product_id');
}
在您的產(chǎn)品模型中:
public function currencies()
{
return $this->hasMany(\App\Currency::class, 'product_id', 'id');
}
所以你可以這樣稱呼它:
RAB::with(['products' => function($query) {
$query->with('currencies');
}])->get();
不幸的是,foreign_key和數(shù)據(jù)透視表中,您無法直接創(chuàng)建貨幣。productsrabshasmanythrough
您可以按產(chǎn)品調(diào)用貨幣。或者,您可以創(chuàng)建一個透視模型,然后使用 。hasmanythrough
- 1 回答
- 0 關(guān)注
- 112 瀏覽
添加回答
舉報