Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 528
samuel toh Avatar asked Nov 29 '25 16:11

samuel toh


1 Answers

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.

like image 182
Alexey Mezenin Avatar answered Dec 02 '25 06:12

Alexey Mezenin