samuel toh
samuel toh

Reputation: 7076

How to pass array in where condition? Laravel 5.3

If I have data like this

For example, my table is country table

First data :

id = 1; name = england; deleted_by = 1, 3

Second data :

id = 2; name = spain; deleted_by = 2, 4

I try like this :

Country::whereIn('deleted_by', $user_id)->get(); 

But seems my case can not use whereIn

For example, I want to show data has an user_id = 1

How can I do it?

Note :

deleted_by = the data deleted by the user id

$user_id = user id that is being logged

Upvotes: 3

Views: 295

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

That's not how it works. You can do something like this:

Country::where('deleted_by', 'like', ','.$user_id.',')->get();

And store data as ,2,4,

But a much better way is to keep user IDs in a separate pivot table and use many-to-many relationship.

Upvotes: 2

Related Questions