DEV Community

ahmed sabry mahfouz
ahmed sabry mahfouz

Posted on

Laravel: whereIn() vs whereIntegerInRaw() vs find()

Salam Alikom guys
if you use whereIn() for a query on integer values like this:
Post::whereIn('id',[1,2,3])->get() then there are other good choices to do this:

  • the find() method yes it accepts array of ids also Post::find([1,2,3])

  • the whereIntegerInRaw() method like this:
    Post::whereIntegerInRaw('id',[1,2,3])->get() also there is another version like whereNotIn which is whereIntegerNotInRaw().

And here are the performance results of using these methods to get data in id using 10000 ids :

  • find(rang(1,10000)) : 30 ms

  • whereIntegerInRaw('id',range(1,10000)): 34 ms

  • whereIn('id', range(1,10000)) : 500 ms

That's it!
Thank you :)

Top comments (0)