DEV Community

Morcos Gad
Morcos Gad

Posted on

5 2

insertOrIgnore to Ignore SQL Errors - Laravel

I found this tip and wanted to share it with you so that you can benefit from it in your next projects, which is if you are not sure that this row exists in the database and want to avoid sending an error message when this row exists here, use insertOrIgnore

Laravel InsertOrIgnore With Eloquent

DB::table('users')->insertOrIgnore($data);
Enter fullscreen mode Exit fullscreen mode

Laravel InsertOrIgnore Query With Eloquent Model

User::insertOrIgnore($data);
Enter fullscreen mode Exit fullscreen mode

I hope you benefited and enjoyed this code with him. If you want to dig deeper, visit these sources
https://www.mysqltutorial.org/mysql-insert-ignore/
https://www.tutsmake.com/laravel-eloquent-insertorignore-example/
https://www.youtube.com/watch?v=hqiUzO8EFbM

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay