我是 Laravel 的初学者。我在使用 Laravel 时遇到了一些小问题。我有这个代码:$categories = $categoryRepository->descendantsAndSelf($slug ?? null); 这个返回: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 ]}还有我的存储库: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(); }当我运行我的代码时:dd($this->model->getProductFromIdCategories($categories->toArray()));
1 回答
当年话下
TA贡献1890条经验 获得超9个赞
您有关系“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 关注
- 117 浏览
添加回答
举报
0/150
提交
取消