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.
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 ?
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.
#laravel #eloquent-query