Insert gagal ke database

Controller
    public function tambah(Request $request){
        $satuan = $request->satuan;
        $nama   = $request->nama_br;

        if($satuan == "Kg"){
            $query = DB::table('barang')
            ->select('id_barang', 'harga_kg')
            ->where('nama_barang','like',"%".$nama."%")
            ->get('id_barang','harga_kg');
            
            $pesan = new DetailPesanan();
            $pesan ->kode_tr = $request->kdpembelian;
            $pesan ->nama_pembeli = $request->nama_pb;
            $pesan ->no_hp = $request->no_hp;
            $pesan ->alamat = $request->alamat;
            $pesan ->id_barang = $query->id_barang; 
            $pesan ->nama_barang = $request->nama_br;
            $pesan ->satuan = $request->satuan;
            $pesan ->harga = $query->harga_kg;
            $pesan ->quantity = $request->jml;
            $pesan ->total = $query->harga_kg*$request->jml;
            $pesan->save();
            
        }
        elseif ( $satuan == "Ton"){

                $query2 = DB::table('barang')
                ->select('id_barang', 'harga_ton')
                ->where('nama_barang','like',"%".$nama."%")
                ->get('id_barang','harga_ton');
                
                $pesan = new DetailPesanan();
                $pesan ->kode_tr = $request->kdpembelian;
                $pesan ->nama_pembeli = $request->nama_pb;
                $pesan ->no_hp = $request->no_hp;
                $pesan ->alamat = $request->alamat;
                $pesan ->id_barang = $query2->id_barang; 
                $pesan ->nama_barang = $request->nama_br;
                $pesan ->satuan = $request->satuan;
                $pesan ->harga = $query2->harga_ton;
                $pesan ->quantity = $request->jml;
                $pesan ->total = $query2->harga_ton*$request->jml;
                $pesan->save();
                
            }
        

        return redirect('/HasilPesanan');
    }

Model
function Barang(){
        return $this->hasMany('App\Models\Barang');
    }

    protected $table = 'detail_pesanan';
    protected $keyType = 'integer';
    protected $fillable = ['kode_tr','nama_pembeli','no_hp','alamat','id_barang','nama_barang','satuan','harga','quantity','total'];


    public static function kode_tr()
    {
        $kode = DB::table('detail_pesanan')->max('kode_tr');
        $addNol = '00';
        $kode = str_replace("TR", "", $kode);
        $kode = (int) $kode + 1;
        $incrementKode = $kode;

        $kodeBaru = "TR".$addNol.$incrementKode;
        return $kodeBaru;
    }



Views

 <div class="row">
                   <div class="col-lg-6">
                    <div class="card">
                        <div class="card-header">
                            <strong class="card-title">Identitas Pembeli</strong>
                        </div>
                        <div class="card-body">
                          <div class="panel-body">
                            <table class="table table-striped">
                              <tbody>
                                <tr>
                                  <td><label>Kode Pembelian</label></td>
                                </tr>
                                <tr>
                                <td><input type="text" class="form-control" name="kdpembelian" id="kdpembelian" maxlength="8" readonly="true" value="{{ $kode_tr }}">
                      </td>
                                </tr>
                                <tr>
                                  <td><label for="cc-name" class="control-label mb-1">Nama Pembeli</label></td>
                                </tr>
                                <tr>
                                <td><input type="text" class="form-control" name="nama_pb" id="nama_pb" placeholder="Masukan Nama Lengkap"></td>
                                </tr>
                                <tr>
                                <td><label for="cc-name" class="control-label mb-1">Nomor Handphone</label></td>                                </tr>
                                <tr>
                                  <td>
                                  <input type="text" class="form-control" name="no_hp" id="no_hp" placeholder="Masukan Nomor Handphone">
                                 </td>
                                </tr>
                                <tr>
                                <td><label for="cc-name" class="control-label mb-1">Alamat</label></td>                                </tr>
                                <tr>
                                  <td>
                                  <input type="text-area" class="form-control" id="alamat" name="alamat" placeholder="Masukkan Alamat">
                                 </td>
                                </tr>
                            </tbody>
                            </table>       
                   </div>
                        </div>   
                    </div>
                  </div><!--/.col-->

                <div class="col-lg-6">
                    <div class="card">
                        <div class="card-header"><strong>Pembelian Barang</strong></div>
                         <div class="card-body card-block">
                        <div class="panel-body">
                            <table class="table table-striped">
                              <tbody>
                                <tr>
                                <td><label for="cc-name" class="control-label mb-1">Nama Barang</label></td>                                </tr>
                                <tr>
                                  <td>
                                  <select name="nama_br" id="nama_br" class="form-control">
                                        @foreach($barang as $br)
                                            <option value="{{ $br->nama_barang }}"> {{ $br->nama_barang }}</option>
                                        @endforeach
                                    </select>
                                 </td>
                                </tr>
                                <tr>
                                  <td><label for="cc-name" class="control-label mb-1">Satuan</label></td>
                                </tr>
                                <tr>
                                  <td><div class="col-sm-7">
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="kuerating" id="kuerating" value="Kg">
                                            <label class="form-check-label" for="inlineRadio1">KG</label>
                                        </div>
                                        <div class="form-check form-check-inline">
                                            <input class="form-check-input" type="radio" name="kuerating" id="kuerating" value="Ton">
                                            <label class="form-check-label" for="inlineRadio2">Ton</label>
                                        </div>                            
                                 </td>
                                </tr>
                                <tr>
                                  <td><label for="cc-name" class="control-label mb-1">Jumlah Beli</label></td>
                                </tr>
                                <tr>
                                  <td>
                                  <input type="text" class="form-control" name="jml" id="jml" placeholder="Masukan Nama Barang">
                                 </td>
                                </tr>
                              </tbody>
                            </table>
                      </div>
                    </div>
                  </div>
            </center>
</div>
</div>
<br>
                    <center>
                    <div class="form-actions form-group">
                        <div class="panel-footer" >
                            <button id="formbtn" class="btn btn-primary" name="save"><i class="fa fa-save"></i> Simpan</button>
                     </div>   
                        </div>
                        </center> 
avatar whreq16

@whreq16

2 Kontribusi 2 Poin


Jawaban

Belum ada Jawaban, jadi yang pertama

Login untuk gabung berdiskusi