Skip to content
Advertisement

how to save new data and update data in a different table with laravel

How can i save a record new and update data in 2 different tables in laravel. I have perpustakaan table for to update. My second table is called peminjaman_buku for save new data. I hope you can help me, thank you very much

controller :

public function store(Request $request)
    {
        $request->validate([
            'kode_buku ' => 'required',
            'kode_peminjam' => 'required',
            'petugas_pinjam' => 'required',
            'tanggal_pinjam' => 'required',
        ]);
        
        $status = "pinjam";
        $model = new P_Peminjaman;
        $model->kode_buku = $request->judul_buku;
        $model->kode_peminjam = $request->kode_peminjam;
        $model->petugas_pinjam = $request->petugas_pinjam;
        $model->tanggal_pinjam = $request->tanggal;
        $model->status = $status;
        
        $model->save();
        
        $modal = M_Perpustakaan::find($judul_buku);
        $modal->kode_buku = $request->judul_buku;
        $modal->status = $status;
        
        $modal->save();
        return redirect('ppeminjaman');

    }

when I do the process there is no updated data or new data in the database

Advertisement

Answer

Change your modal code from this

$modal = M_Perpustakaan::find($judul_buku);
        $modal->kode_buku = $request->judul_buku;
        $modal->status = $status;
        
        $modal->save();

To this

$modal = M_Perpustakaan::find($judul_buku);
        $modal->update([
            'kode_buku' => $request->judul_buku,
            'status' => $status,
        ]);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement