Home >Database >Mysql Tutorial >How to Efficiently Retrieve Product Data in Laravel Using a Subquery in `WHERE IN`?
Laravel Subquery WHERE IN:
You're seeking a performant way to retrieve product data from the products table in Laravel, utilizing a subquery to specify the product_id values to include. While a join alternative exists, you prefer a subquery-based approach for optimization purposes.
Solution:
Below is the code that addresses your requirement, leveraging subqueries within the whereIn clause:
Products::whereIn('id', function($query){ $query->select('paper_type_id') ->from(with(new ProductCategory)->getTable()) ->whereIn('category_id', ['223', '15']) ->where('active', 1); }) ->get();
Explanation:
The above is the detailed content of How to Efficiently Retrieve Product Data in Laravel Using a Subquery in `WHERE IN`?. For more information, please follow other related articles on the PHP Chinese website!