Laravel Eloquent insertOrIgnore() Query Example

Admin   Laravel   279  2021-03-15 19:10:02

Hi Guys,

In this blog, I would like to share with you how to use insertorignore query in laravel application.

The use of insertOrIgnore query in laravel used to insert record but ignore duplicate record while inserting.

The insertOrIgnore method will ignore duplicate record errors while inserting records into the database.

Here I will give two example for laravel insertOrIgnore query. So let's see the bellow example.

Example 1 : Query

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

DB::table('users')->insertOrIgnore([

['name' => 'taylor', 'email' => '[email protected]'],

['name' => 'dayle', 'email' => '[email protected]'],

]);

}

Example 2 : Query

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

User::insertOrIgnore([

['name' => 'taylor', 'email' => '[email protected]'],

['name' => 'dayle', 'email' => '[email protected]'],

]);

}

It will help you...