1

I am using Laravel 5.5,I want to exclude duplicate records when inserting a group of data into mysql.

For example,there is a table students,it has these fields:

id
name
gender

Now I will insert a group of data into students,if not mind duplicate records,I can do it like this:

public function insert()
{
    $newStudents=[
        ['name'=>'Jim','gender'=>'boy'],
        ['name'=>'Lucy','gender'=>'girl'],
        ['name'=>'Jack','gender'=>'boy'],
        ['name'=>'Alice','gender'=>'girl']
    ];

    DB::table('students')->insert($newStudents);
}

Now I don't want to insert duplicate records.(The duplicate is : both name and gender have the same values,not one field has the same value).

what should I do?

zwl1619
  • 3,118
  • 10
  • 37
  • 87

2 Answers2

2

You could use the collection helper unique. See code below:

 $newStudents=collect([
    ['name'=>'Jim','gender'=>'boy'],
    ['name'=>'Lucy','gender'=>'girl'],
    ['name'=>'Jack','gender'=>'boy'],
    ['name'=>'Alice','gender'=>'girl'],
     ['name'=>'Alice','gender'=>'girl']
    ])->unique(function ($student) {
        return $student['name'].$student['gender'];
    })->toArray();

    DB::table('students')->insert($newStudents);

The above code will only insert unique records, even though there is a duplicate record there.

For more information, see here:https://laravel.com/docs/5.4/collections#method-unique

pseudoanime
  • 1,397
  • 10
  • 17
  • if table `students` already has the same record,how to do it ? for example, assuming table `students` has a record `['name'=>'Jack','gender'=>'boy']` already . now in `$newStudents` ,there is also a `['name'=>'Jack','gender'=>'boy']` ,what should I do? – zwl1619 Sep 06 '17 at 19:38
0

You could create an unique index for the name and gender in the database. However, then when you try to save them you will get a MySQL error I'm guessing. So you could use the unique validation with where.

Validator::make($student, [
    'gender' => [
        'required',
        Rule::in(['boy', 'girl']),
    ],
    'name' => Rule::unique('students')->where(function ($query) use ($student) {
        $query->where('gender', $student['gender']);
    })
]);

Then you can run your collection through and filter out the ones that aren't valid like in @pseudoanime's answer.

Pitchinnate
  • 7,307
  • 1
  • 18
  • 35