Reputation: 295
I've been trying to do this mysql equivalent in laravel:
SELECT * FROM toys
WHERE type_id In (1,2,3)
here is my Controller code:
public function index(Request $request)
{
$type = (new Type)->newQuery();
if($request->has('type_id')){
$type->whereIn('type_id',$request->type_id));
}
return $type->paginate(10);
}
The idea is to be able to query data from db in URL: localhost/toyslist?type_id=1,2,3
Been getting Invalid argument supplied foreach()
Any help is much appreciated!
Upvotes: 1
Views: 1127
Reputation: 941
Use explode to convert a string into Array
public function index(Request $request)
{
$type = (new Type)->newQuery();
if($request->has('type_id')){
$typeArray = explode(",",$request->type_id)
$type->whereIn('type_id',$typeArray ));
}
return $type->paginate(10);
}
Upvotes: 3