Reputation: 263
I am using WhereIn()
method in Laravel to fetch various 'id', but the problem is the data am getting is like this ["6,19"]
what am i expecting is an array with numbers without double quotes.
The data am getting is from a varchar field so, it comes as varchar and now i need to change that as [6,19] without double quotes. Please guide me to do so.
The whereIn
method supports only array with numbers.
public function getusers($id)
{
$users = User::where('member_id', $id)->get();
foreach($users as $user)
{
$s = $user->path;
$s = str_replace('/', ',', $s); //this gives 6,19 and if i wrap that with [] it gives us ["6,19"].
return $usergs = User::whereIn("id", [$s])->get(); //[$s] gives like ["6,19"] -but i need to change that as [6,19] (array not string)
}
}
Upvotes: 1
Views: 811
Reputation: 26450
You can explode on /
instead, which makes it an array. You can then pass that array into whereIn()
without needing to replace anything.
public function getusers($id)
{
$users = User::where('member_id', $id)->get();
foreach($users as $user) {
$s = explode("/", $user->path);
return $usergs = User::whereIn("id", $s)->get();
}
}
Upvotes: 2