DEV Community

ahmed sabry mahfouz
ahmed sabry mahfouz

Posted on

1

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)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay