我想知道我如何能夠計算出所有配方中成分的發生頻率。如何在Laravel 5中的數據透視表中計數
因爲我使用withPivot
這很難實現(我認爲)。
型號成份:
class Ingredient extends Model
{
public function receipt()
{
return $this->belongsToMany('Receipt','receipt_ingredients','ingredient_id','receipt_id')->withPivot('amount');
}
}
型號收據
class Receipt extends Model
{
public function ingredients()
{
return $this->belongsToMany('Ingredient','receipt_ingredients','receipt_id','ingredient_id')->withPivot('amount');
}
}
型號ReceiptIngredient(我的透視表)
class ReceiptIngredient extends Model
{
public $table = 'receipt_ingredients';
public function receipt()
{
return $this->belongsTo('Receipt');
}
public function ingredient()
{
return $this->belongsTo('Ingredient');
}
}
最後,我想列出每種配料的配方中出現的次數。
爲「receipt_ingredients」
我不認爲你需要創建一個型號爲支點,也如果我們folllow的laravel約定透視表應命名爲'ingredient_receipt' – user3813360
哦,你」對了!我應該重命名它。謝謝! 也許有可能使用這個數據透視模型來計算這些東西 - 我必須嘗試一下。 – zer0