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

vandan's avatar
Level 13

how to check array value in whereIn

hello guys, i m stuck in whereIn query

here is my services array like [1,3] and i want to check if 1 or 3 in user table then display services, so i create query like this

	$services = Services::pluck('id')->toArray();   
	// output be like 
		array:2 [0 => 1 
				1 => 3 ]
	$user = User::whereIn('services_id',$services)->get();

my user table services_id like [1,3] in database but whereIn cannot check in array so please can you guide me how to do it?

0 likes
10 replies
vandan's avatar
Level 13

@frankielee not this way because i need like in user table [1,3] so when i services_id 1 then result should be display in 1 services_id not like full json so example like [1,3] so 1 or 3 in array search

sr57's avatar

@vandan

What db are you using?

and please share your migration.

1 like
vandan's avatar
Level 13

@sr57 yes sure

	public function up()
	{
    		Schema::table('users', function (Blueprint $table) {            	                  
        		$table->string('services_id')->nullable()->after('subscription_id');
    		});
	}

	 protected $casts = [
    	'skill' => 'array',
		'services_id' => 'array'
];
sr57's avatar

@vandan

How do you store service_id in the string field type services_id?

Please share sample data.

1 like
sr57's avatar

@vandan

in addition to @snapey 's answer, don't you have any data to share? Are you going to define your structure? Maybe you have to rephrase your initial post?

Snapey's avatar

you cannot treat a column as an array unless you store it as json type

but this is a really bad strategy. the user services should be stored as a pivot table and a many to many relationship

1 like

Please or to participate in this conversation.