How to do this in Laravel, subquery where in

PhpMysqlLaravelEloquentSubquery

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

Consider this code:

Products::whereIn('id', function($query){
	$query->select('paper_type_id')
	->from(with(new ProductCategory)->getTable())
	->whereIn('category_id', ['223', '15'])
	->where('active', 1);
})->get();

Solution 2 - Php

Have a look at the advanced where clause documentation for Fluent. Here's an example of what you're trying to achieve:

DB::table('users')
    ->whereIn('id', function($query)
    {
        $query->select(DB::raw(1))
              ->from('orders')
              ->whereRaw('orders.user_id = users.id');
    })
    ->get();

This will produce:

select * from users where id in (
    select 1 from orders where orders.user_id = users.id
)

Solution 3 - Php

You can use variable by using keyword "use ($category_id)"

$category_id = array('223','15');
Products::whereIn('id', function($query) use ($category_id){
   $query->select('paper_type_id')
     ->from(with(new ProductCategory)->getTable())
     ->whereIn('category_id', $category_id )
     ->where('active', 1);
})->get();

Solution 4 - Php

You can use Eloquent in different queries and make things easier to understand and mantain:

$productCategory = ProductCategory::whereIn('category_id', ['223', '15'])
                   ->select('product_id'); //don't need ->get() or ->first()

and then we put all together:

Products::whereIn('id', $productCategory)
          ->where('active', 1)
          ->select('id', 'name', 'img', 'safe_name', 'sku', 'productstatusid')
          ->get();//runs all queries at once

This will generate the same query that you wrote in your question.

Solution 5 - Php

The script is tested in Laravel 5.x and 6.x. The static closure can improve performance in some cases.

Product::select(['id', 'name', 'img', 'safe_name', 'sku', 'productstatusid'])
            ->whereIn('id', static function ($query) {
                $query->select(['product_id'])
                    ->from((new ProductCategory)->getTable())
                    ->whereIn('category_id', [15, 223]);
            })
            ->where('active', 1)
            ->get();

generates the SQL

SELECT `id`, `name`, `img`, `safe_name`, `sku`, `productstatusid` FROM `products` 
WHERE `id` IN (SELECT `product_id` FROM `product_category` WHERE 
`category_id` IN (?, ?)) AND `active` = ?

Solution 6 - Php

The following code worked for me:

$result=DB::table('tablename')
->whereIn('columnName',function ($query) {
                $query->select('columnName2')->from('tableName2')
                ->Where('columnCondition','=','valueRequired');

            })
->get();

Solution 7 - Php

Here is my approach for Laravel 8.x gathered from multiple answers here:

  • Use the query builder and don't write SQL directly.
  • Use the models and determine everything from there. Don't use a hardcoded table name, or any name (columns, and so on) for that matter.
Product::select(['id', 'name', 'img', 'safe_name', 'sku', 'productstatusid'])
    ->whereIn('id', ProductCategory::select(['product_id'])
        ->whereIn('category_id', ['223', '15'])
    )
    ->where('active', 1)
    ->get();

Solution 8 - Php

Laravel 4.2 and beyond, may use try relationship querying:-

Products::whereHas('product_category', function($query) {
$query->whereIn('category_id', ['223', '15']);
});

public function product_category() {
return $this->hasMany('product_category', 'product_id');
}

Solution 9 - Php

Product::from('products as p')
->join('product_category as pc','p.id','=','pc.product_id')
->select('p.*')
->where('p.active',1)
->whereIn('pc.category_id', ['223', '15'])
->get();
	

Solution 10 - Php

using a variable

$array_IN=Dev_Table::where('id',1)->select('tabl2_id')->get();
$sel_table2=Dev_Table2::WhereIn('id',$array_IN)->get();

Solution 11 - Php

Please try this online tool sql2builder

DB::table('products')
    ->whereIn('products.id',function($query) {
                            DB::table('product_category')
                            ->whereIn('category_id',['223','15'])
                            ->select('product_id');
                        })
    ->where('products.active',1)
    ->select('products.id','products.name','products.img','products.safe_name','products.sku','products.productstatusid')
    ->get();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarc BuurkeView Question on Stackoverflow
Solution 1 - PhplukaseratView Answer on Stackoverflow
Solution 2 - PhpdrewjohView Answer on Stackoverflow
Solution 3 - PhpRameshView Answer on Stackoverflow
Solution 4 - PhpPhilipeView Answer on Stackoverflow
Solution 5 - PhpMadan SapkotaView Answer on Stackoverflow
Solution 6 - PhpAditya SinghView Answer on Stackoverflow
Solution 7 - PhpDarkproductView Answer on Stackoverflow
Solution 8 - PhpLC YoongView Answer on Stackoverflow
Solution 9 - PhppanqingqiangView Answer on Stackoverflow
Solution 10 - Phpa3rxanderView Answer on Stackoverflow
Solution 11 - Phpliquid207View Answer on Stackoverflow