Avatar ·

Laravel Eloquent: how to build a complex WHERE with a combination of OR and AND?

Hello everyone! I'm new to Laravel and Eloquent, and I'm having trouble building a complex SQL query with WHERE conditions that use both OR and AND simultaneously. I have a `products` table with fields: `status`, `category_id`, `price`, `is_featured`. I need to select products that satisfy one of two conditions: 1. The product is active (status = 'active') AND has a price less than 1000 (price < 1000); 2. OR the product is featured (is_featured = 1) AND belongs to category with id = 5 (category_id = 5). That is, in SQL it would look like this: ```sql SELECT * FROM products WHERE (status = 'active' AND price < 1000) OR (is_featured = 1 AND category_id = 5); ``` I tried using `where()` and `orWhere()` in Eloquent, but it turns into a mess. For example, like this: ```php $pr