1

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!

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much! I had been trying implode and wasn't working but EXPLODE works!
Although you solved this better you use another table for this and save one record in one column for searching and sorting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.