Home > Back-end >  How to delete all rows which have value 1 in "status_pesanan" column
How to delete all rows which have value 1 in "status_pesanan" column

Time:12-23

I want to delete all rows which have value of 1 in column "status_pesanan" before running create data Laravel

enter image description here

my controller

public function create()
{
    $penjualan = DB::table('penjualan')
            ->groupBy('status_pesanan')
            ->havingRaw('COUNT(status_pesanan) = 1')
            ->delete();

    $penjualan = new Penjualan();    
    $penjualan->nama_pemesan = 1;
    $penjualan['no_nota'] = tambah_nol_didepan($penjualan->no_nota 1, 5);  
    $penjualan->alamat_pemesan = 1;
    $penjualan->telepon_pemesan = 1;
    $penjualan->acc_desain = 1;
    $penjualan->total_item = 0;
    $penjualan->total_harga = 0;
    $penjualan->diskon = 0;
    $penjualan->bayar = 0;
    $penjualan->diterima = 0;
    $penjualan->id_user = auth()->id();
    $penjualan->save();

    session(['id_penjualan' => $penjualan->id_penjualan]);
    return redirect()->route('transaksi-baru.index');
}

If I run the code as above, the output I receive is that all old rows in the database are deleted

CodePudding user response:

You are grouping by status_penasan having its count equal to 1 and call delete upon it. You wanted to remove the records whose status_penasan value is 1. This could be a fix:

    $penjualan = DB::table('penjualan')
            ->where('status_pesanan', 1)
            ->delete();
  • Related