Friday, November 22, 2024
Google search engine
HomeLanguagesLaravel Eloquent insertOrIgnore Example

Laravel Eloquent insertOrIgnore Example

Laravel insertOrIgnore() eloquent method example, you will learn how to use insertorignore query in laravel application.

Whenever you want to insert records in bulk in database table. But you want the records that already exist in the database table. Don’t have a duplicate entry. So you can use insertOrIgnore() eloquent query in laravel apps.

So this post will guide how to use insertOrIgnore query in laravel. Because this is used to insert records but ignore duplicate records while inserting records into DB table in laravel application.

Laravel insertOrIgnore Eloquent Example

Here, you will learn how to use insertOrIgnore() Eloquent method with queries in laravel. You can see the following examples of insertOrIgnore() eloquent method:

Example 1: Laravel InsertOrIgnore With Eloquent

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
   $array = [
		      ['name' => 'taylor', 'email' => '[email protected]'],
		      ['name' => 'dayle', 'email' => '[email protected]'],
            ];

    DB::table('users')->insertOrIgnore($array);
}         

Note that, Laravel insertOrIgnore method will ignore duplicate record errors while inserting records into the database.

Example 2: Laravel InsertOrIgnore Query With Eloquent Model

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
   $array = [
		      ['name' => 'taylor', 'email' => '[email protected]'],
		      ['name' => 'dayle', 'email' => '[email protected]'],
            ];

    User::insertOrIgnore($array);
}         

Conclusion

In this laravel insertOrIgnore() eloquent method with example, you have learned how to use insertOrIgnore query with eloquent and model in laravel. And insertOrIgnore method will ignore duplicate record errors while inserting records into the database table in laravel apps.

RELATED ARTICLES

Most Popular

Recent Comments