Home > Back-end >  Laravel DB updated 2 row even 1 id given
Laravel DB updated 2 row even 1 id given

Time:06-12

I don't know what's wrong with my code, i want to update 1 row of my DB, but instead it's updated 2 row at once, i'm using ajax post to update the data

ajax query :

$('#tolak').on('click', function() {
                const id = $(this).attr('data-id')
                const name = $(this).attr('data-name')
                Swal.fire({
                    title: 'Konfirmasi',
                    text: `Apakah anda yakin akan menolak form pendaftaran driver ${name}`,
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Ya, tolak!',
                    cancelButtonText: 'Batal',
                }).then((result) => {
                    Swal.showLoading()
                    if (result.isConfirmed) {
                        $.ajax({
                            url: '{{ route('admin.verifDriver') }}',
                            type: 'ajax',
                            method: 'post',
                            data: {
                                id: id,
                                status: 2,
                            },
                            success: function(data) {
                                console.log(data)
                                Swal.fire(
                                    'Sukses!',
                                    `Berhasil tolak pendaftaran ${name}`,
                                    'success'
                                )
                                fetchData()
                                Swal.hideLoading()
                            },
                            error: function(e) {
                                Swal.hideLoading()
                            }
                        })
                    }
                })
            })

And here's the controller

public function verif(Request $req)
    {
        DB::table('drivers')
            ->where('id', $req->id)
            ->update([
                'status' => $req->status
            ]);
    }

it's just a simple code, i have done it before on the other project but it works, i don't know what's wrong with this code.

CodePudding user response:

Try to update with eloquent model instead of DB.

public function verif(Request $req)
{
    Driver::where('id', $req->id)
        ->update([
            'status' => $req->status
        ]);
}

Dont forget to import Driver namespace.

CodePudding user response:

I believe that you have multiple rows in the view file. So add 'tolak' to the class attribute. So ajax is triggering multiple times since you used id . Make sure the id attribute is always unique in dom. So change ajax like below.

$('.tolak').on('click', function(e) {
    e.preventDefault();
                const id = $(this).attr('data-id')
                const name = $(this).attr('data-name')
                Swal.fire({
                    title: 'Konfirmasi',
                    text: `Apakah anda yakin akan menolak form pendaftaran driver ${name}`,
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Ya, tolak!',
                    cancelButtonText: 'Batal',
                }).then((result) => {
                    Swal.showLoading()
                    if (result.isConfirmed) {
                        $.ajax({
                            url: '{{ route('admin.verifDriver') }}',
                            type: 'ajax',
                            method: 'post',
                            data: {
                                id: id,
                                status: 2,
                            },
                            success: function(data) {
                                console.log(data)
                                Swal.fire(
                                    'Sukses!',
                                    `Berhasil tolak pendaftaran ${name}`,
                                    'success'
                                )
                                fetchData()
                                Swal.hideLoading()
                            },
                            error: function(e) {
                                Swal.hideLoading()
                            }
                        })
                    }
                })
            })
  • Related