Моя проблема: таблица продуктов содержит 40 000 строк, а таблица Product_details — 2,1 миллиона строк.
Я хочу отфильтровать таблицу продуктов с помощью таблицы Product_details
мой код
Код: Выделить всё
$sliced = array_values(array_slice($req_val, 3));
$products = Products::where('status', 1)
->where('s_part', $req_val[0])
->where('main_category', $req_val[1])
->where('sub_category', $req_val[2])
->join('product_details', 'products.sapcode', '=', 'product_details.sapcode')
->where(function ($query) use ($sliced, $keys) {
foreach ($sliced as $index => $value) {
$key = $keys[$index];
$query->orWhere(function ($query) use ($value, $key) {
$query->where('product_details.value', $value)
->where('product_details.name', $key);
});
}
})
->get('products.sapcode');
$products = $products->map(function($item){
return $item->sapcode;
});
$counts = array_count_values($products->toArray());
$threshold = count($sliced);
$filtered = array_filter($counts, function($count) use ($threshold) {
return $count >= $threshold;
});
$result = array_keys($filtered);
$products = Products::whereIn('products.sapcode', $result)->get();
return response()->json($products);
Product_details связана с Product_details
Как соединить и отфильтровать эти две таблицы. Как лучше всего это сделать.
Подробнее здесь: https://stackoverflow.com/questions/784 ... ther-table
Мобильная версия