Laravel Pass Array whereIN clause Example using query builder

Today, We want to share with you Laravel Pass Array whereIN clause Example using query builder.In this post we will show you Laravel Pass variable Into MysqL whereIN clause, hear for laravel whereIN operator dynamic Set dynamic variable we will give you demo and example for implement.In this post, we will learn about Laravel 5 – whereIn and whereNotIn with subquery example using query builder with an example.

Laravel Pass Array whereIN clause Example using query builder

There are the Following The simple About Laravel Pass Array whereIN clause Example using query builder Full Information With Example and source code.

As I will cover this Post with live Working example to develop Pass a Laravel array into Mysql ‘whereIN’ clause, so the Laravel Pass Array of Strings to whereIN clause in Mysqlfor this example is following below.

Read Also:  jQuery Disable Mouse Right Click Examples

Example 1 : whereIN clause Dynamic Set In Laravel

Laravel Pass variable Into MysqL whereIN clause

$all_assign_category = json_decode(Auth::user()->category_ids);
$table ->whereIn("tickets.category_id", array_flatten($all_assign_category));

Full Example for laravel whereIN operator dynamic Set dynamic variable

//set Primary Tables name
$sTable = "members";

$table = DB::table($sTable);

//get id's
$all_assign_category = json_decode(Auth::user()->category_ids);

$table->join('priorities', 'members.priority_id', '=', 'priorities.id');
$table->join('categories', 'members.category_id', '=', 'categories.id');
$table->join('products', 'members.product_id', '=', 'products.id');
$table->join('roles', 'members.role_id', '=', 'roles.id');
$table->select('members.*', 'priorities.name AS pname', 'categories.name AS cname', 'products.name as subname', 'roles.name as rolename');
$table->whereNull('members.deleted_at');
$table ->whereIn("members.category_id", array_flatten($all_assign_category));
$table->orderBy('members.id', 'desc');

Example 2 : Laravel whereIn with subquery example

using SQL Query:

SELECT * FROM `members`
	WHERE `id` IN (
		SELECT `member_id` FROM `invite_members`
		)

using Laravel Query:

$users = DB::table("members")->select('*')
    ->whereIn('id',function($query){
       $query->select('member_id')->from('invite_members');
    })
    ->get();

Example 3 : Laravel Where Not In Query with subquery example

using SQL Query:

SELECT * FROM `members`
	WHERE `id` NOT IN (
		SELECT `member_id` FROM `invite_members`
		)

using Laravel Query:

$users = DB::table("members")->select('*')
            ->whereNOTIn('id',function($query){
               $query->select('member_id')->from('invite_members');
            })
            ->get();

Example 4 : Using PHP

Passing an array to a query using a WHERE clause

Read Also:  Laravel Pagination Example Tutorial

$ids = join("','",$members);
$sql = "SELECT * FROM members WHERE id IN ('$ids')";
print_r($sql);

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Pass Array whereIN clause Example using query builder.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.