Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete all rows which have value 1 in "status_pesanan" column

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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();
1 comments

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading