0
我有一個非常奇怪的問題,與我多對多的關係。Laravel - 關係數據缺失
此功能:
$ingredients = Ingredients::with(array('recipes' => function ($q) {
$q->where('recipes.status', '=', '1')
->join('votes','recipes.id','=','votes.recipe_id')
->select(DB::raw('avg(rating) AS rating'))->groupBy('recipes.id')->orderBy('rating', 'DESC');
}))
->where('ingredients.is_product',1)
->get()->toArray();
應該讓我所有的配料和食譜吧。問題是,一些配料食譜缺失。
該加入不起作用。
我在recipe_id = 1和= 2中添加了recipe_id = 7,它只在ingredient_id = 1中顯示此配方,但不在2中。
如果我更改主元ID(假設成分= 1是1並且2是在2),我改變成分_ID 1 2和副verca,與7的食譜是在ingredient_id = 2,但不是在1了。
但它應該在兩個成分。爲什麼? 這就像連接認爲「好吧,我已經添加到ID之前,所以跳過這個ID」。
我的模型:
Recipe.php
public function ingredients() {
return $this->hasMany('Ingredients','ingredients_recipe','recipe_id','ingredients_id')->withPivot('amount');
}
Ingredients.php
public function recipes() {
return $this->belongsToMany('Recipe','ingredients_recipe','ingredients_id','recipe_id')->withPivot('amount');
}
編輯#1:
好吧,我想通了,如果我刪除函數()爲投票,它工作正常。那裏發生了什麼?我需要爲每個配方評分。