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?
Since it is an array, I assumed the column type is JSON
You need to use the JSON method, https://laravel.com/docs/9.x/queries#json-where-clauses
Make sure you meet the condition to use the method:
Currently, this includes MySQL 5.7+, PostgreSQL, SQL Server 2016, and SQLite 3.9.0 (with the JSON1 extension).
@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
@vandan
What db are you using?
and please share your migration.
@vandan
and your migration/table structure?
@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'
];
@vandan
How do you store service_id in the string field type services_id?
Please share sample data.
@sr57 using typecast please check my model
@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?
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
Please sign in or create an account to participate in this conversation.