Laravel 8 根据类别 id 数据获取产品

时间:2021-01-21 08:45:08

标签: php laravel-8

我有一个包含类别 id、name 和 parent_id 的类别表,我有一个产品表,其中包含一个 category_id 列,该列存储多个类别 id,如 ["1", "2", "4"]。

现在我在从具有 id 列的产品表中获取产品详细信息时遇到问题。

$category = Category::where('slug', $this->catslug)->get()->first();
        $data = array();
        foreach($category as $cat){
            $data['category'] = $cat->id;
            $data['products'] = DB::table('products')
                                ->whereJsonContains('category_id', json_decode($cat->id))
                                ->inRandomOrder()->limit(8)->get();
        }
dd($data);

0 个答案:

没有答案
相关问题