How to Avoid Duplicate Entry in Database in Laravel



In this quick example i will discuss about insertOrIgnore query example. I will explain it with eloquent orm and also with query builder. You will learn how to use insertorignore query with query builder or eloquent orm in laravel application.

Using insertOrIgnore we can easily remove duplicate content. So now see how to avoid duplicate entry in database in laravel application. So we will insert data laravel insert if not exists. We will ignore duplicate records while inserting records into DB table in laravel application using insertOrIgnore.

Laravel InsertOrIgnore With Eloquent

public function index()
{
   $array = [
              ['name' => 'taylor', 'email' => 'taylor@example.com'],
              ['name' => 'dayle', 'email' => 'dayle@example.com'],
            ];
 
    DB::table('users')->insertOrIgnore($array);
}   

 

Recommended:- How to Use groupBy() having() and havingRaw() with DB::raw in Laravel ?

Laravel InsertOrIgnore Query With Eloquent Model

public function index()
{
   $array = [
              ['name' => 'taylor', 'email' => 'taylor@example.com'],
              ['name' => 'dayle', 'email' => 'dayle@example.com'],
            ];
 
    User::insertOrIgnore($array);
} 

  

In this quick laravel insertOrIgnore() eloquent method example tutorial, you have learned how to use insertOrIgnore query with eloquent and model in laravel. Hope it can help you.

 

Facebook Github
A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using Laravel Vue and React Js Application.