where-in

How to do this in Laravel, subquery where in

三世轮回 提交于 2019-11-26 01:58:25
问题 How can I make this query in Laravel: SELECT `p`.`id`, `p`.`name`, `p`.`img`, `p`.`safe_name`, `p`.`sku`, `p`.`productstatusid` FROM `products` p WHERE `p`.`id` IN ( SELECT `product_id` FROM `product_category` WHERE `category_id` IN (\'223\', \'15\') ) AND `p`.`active`=1 I could also do this with a join, but I need this format for performance. 回答1: Consider this code: Products::whereIn('id', function($query){ $query->select('paper_type_id') ->from(with(new ProductCategory)->getTable()) -

Can I bind an array to an IN() condition?

試著忘記壹切 提交于 2019-11-25 21:35:16
问题 I\'m curious to know if it\'s possible to bind an array of values to a placeholder using PDO. The use case here is attempting to pass an array of values for use with an IN() condition. I\'d like to be able to do something like this: <?php $ids=array(1,2,3,7,8,9); $db = new PDO(...); $stmt = $db->prepare( \'SELECT * FROM table WHERE id IN(:an_array)\' ); $stmt->bindParam(\'an_array\',$ids); $stmt->execute(); ?> And have PDO bind and quote all the values in the array. At the moment I\'m doing: