Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

V9द's avatar
Level 3

Create And-OR - And Query with Elequent

How we can create sql query in laravel using Elequent

select * from `products` where `status` = ? and `collection` = ? and (`product_category`= ? OR product_category=?) and  (`designer`=? OR `designer`=?) order by `id` desc limit 15 offset 0
0 likes
1 reply
_Artak_'s avatar
_Artak_
Best Answer
Level 14
// Model Product

$productObj = new Product;

$products = $productObj->where('status', '=',$status)
                ->where('collection', '=',$collection)
                ->where(function($query) use($product_category1,$product_category2){
                            // and (`product_category`= ? OR product_category=?)
                            $query->where('product_category', '=',$product_category1)
                                ->orWhere('product_category', '=',$product_category2)
                })
                ->where(function($query1)use ($data){
                    // and  (`designer`=? OR `designer`=?)
                    // do same as above
                })
                ->orderBy($columnName, $ascOrDesc)
                ->take($countTake)
                ->skip($countSkip)
                ->get();

Please or to participate in this conversation.