我有一个包含类别 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);