I am trying to get a list of registered users that are on the contact list of a user. Since many users save phone numbers without the international prefix so I can’t use whereIn
. I am using the last 7 digits of the user’s contact phone numbers to compare with phone numbers of users in the database to return registered users on a user’s contact.
my code looks like this
public function getUserContact($userNumbers){ $numbers = array($userNumbers); $regUsers = User::where(function($query) use($numbers){ foreach($numbers as $userNumber){ $query->orWhere('phone_number', 'LIKE', '%'.$userNumber.'%'); } }); return $regUsers->get(); }
My $numbers
array looks like this
"data": Array [ "060738,3952483,953333,5953333,3879009" ],
but I keep getting an empty array as response. What am I doing wrong
Advertisement
Answer
You’re $numbers
variable is an array with one string of comma delimited numbers, you should be exploding them, not wrapping in an array, try this instead:
public function getUserContact($userNumbers) { $numbers = explode(",", $userNumbers); $regUsers = User::where(function($query) use ($numbers) { foreach($numbers as $userNumber) { $query->orWhere('phone_number', 'LIKE', '%'.$userNumber.'%'); } }); return $regUsers->get(); }