[Vuejs]-Duplicate keys detected: 'X'. This may cause an update error. in VueJS datatable search

3πŸ‘

βœ…

The sample data for single user has duplicate id values in certificate_matrix array (2nd and 11th record both have id of 443)

This would probably be causing an issue in the v-for loop at (where you do not have any :key actually)

<!--Certificates-->
<div class="text-certstyle-titles">
    <div v-for="certificate in selectedEmployee.certificate_matrix"
         v-if="selectedEmployee.certificate_matrix.length > 0"
         class="table--row flex items-center justify-between border-certstyle-border last:border-b-0 border-b px-10 py-4">

Or at any other place where certificate_matrix records are being looped over

Using composite key combining value of two fields to generate unique key

One way to try to ensure unique keys on frontend is to have a combination (kind of like composite key index) as ${id}-${matrix_status} if that combination is to remain unique.

Using loop iteration index as composite suffix to generate unique key

If there is no other field value which can be combined to ensure unique keys for child nodes within v-for then the ${id}-${index} can be used to generate unique keys – eg:

<div v-for="(certificate, index) in selectedEmployee.certificate_matrix"
     :key="`${certificate.id}-${index}`" //To ensure uniqueness of key
     v-if="selectedEmployee.certificate_matrix.length > 0"
     class="table--row flex items-center justify-between border-certstyle-border last:border-b-0 border-b px-10 py-4">
Using uuid as unique key

Another way is to add a uuid column on the database tables and then use the uuid as key in the <DataTable /> component markup including subcomponents where :key or :key-id are used.

Laravel makes it very easy to generate Uuid v4 – uuids via (string) Str::orderedUuid(); And also has $table->uuid('uid')->index() column type for migration files.

Can even have uid stored when a new model record is being created/persisted

// In any model class which has a uuid column
public static function booted()
{
    static::creating(function ($model) {
        $model->uid = Str::orderedUuid()->toString();
    });
}

Laravel Docs – Helpers – Str Ordered Uuid

Laravel Docs – Migrations – Uuid Column Type

Laravel Docs – Uuid – Validation Rule

Update

Based on feedback via comment

Thank you for your answer, I have updated the question with the correct backend controller. The reason for id duplication is one user can have multiple departments. Because of that my eloquent gives me results with multiple user id’s for some results. When I tried to group the results by user.id, it gives me an 500 error, and I need to pass the user.id as the key rather than passing uuid since the id has used for other functions, like user deletion and all… Any way to group the result from the backend to get only unique user ids?

To ensure only unique user records being sent via response from the controller, filter out duplicate records for the $users collection via unique() method available on Collection

//...method code
$users->setCollection($users->unique('id')->values());

return response()->json($users);
πŸ‘€Donkarnash

0πŸ‘

The error message tells you that you have duplicates and you are confused about the reason of getting such an error message, when the id of the user is unique (probably a primary key). However, it is important to note that you load your users with departments and if a user has multiple departments, then you will get that user multiple times. You can group the results to solve the issue.

πŸ‘€Lajos Arpad

Leave a comment