我是 Laravel 的初學(xué)者。我在使用 Laravel 時(shí)遇到了一些小問題。我有這個(gè)代碼:$categories = $categoryRepository->descendantsAndSelf($slug ?? null); 這個(gè)返回:Illuminate\Support\Collection {#535 ▼ #items: array:17 [▼ 0 => 1 1 => 2 2 => 3 3 => 4 4 => 5 5 => 6 6 => 7 7 => 8 8 => 9 9 => 10 10 => 11 11 => 12 12 => 13 13 => 14 14 => 15 15 => 16 16 => 17 ]}還有我的存儲(chǔ)庫:public function getProductFromIdCategories($categories) { return $this->model->select('name', 'slug', 'products.id', 'small_description')->with(['selectedCategory', 'frontImage'])->active()->leftJoin('selected_product_categories', function ($join) { $join->on('products.id', 'selected_product_categories.product_id'); })->whereIn('selected_product_categories.category_id', $categories)->get(); }當(dāng)我運(yùn)行我的代碼時(shí):dd($this->model->getProductFromIdCategories($categories->toArray()));
1 回答

當(dāng)年話下
TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
您有關(guān)系“selectedCategory”。那么為什么要使用左連接呢?使用此代碼
public function getProductFromIdCategories($categories)
{
return $this->model->select('name', 'slug', 'products.id', 'small_description')
->with(['selectedCategory', 'frontImage', 'selectedCategory' => function($q) use ($categories){
$q->whereIn('category_id', $categories);
}])->active()->get();
}
- 1 回答
- 0 關(guān)注
- 143 瀏覽
添加回答
舉報(bào)
0/150
提交
取消