bagimana caranya update stok obat ketika kita sedang membuat transaksi obat pada laravel?

store pada transaksi obat

    public function store(Request $request)
    {
      $id = $request->id;

      $post   =   Obat_tran::updateOrCreate(['id' => $id],
                  [
                      'kode_rekmed' => $request->kode_rekmed,
                      'kode_register' => $request->kode_register,
                      'kd_obat' => $request->kd_obat,
                      'kuantitas' => $request->kuantitas,
                      'subtotal' => $request->kuantitas*$request->harga,
                  ]);

      return response()->json($post);
    }

field pada transaksi obat

    protected $fillable = [
        'kode_rekmed',
        'kode_register',
        'kd_obat',
        'kuantitas',
        'subtotal',
    ];

field pada obat

    protected $fillable = [
        'kd_obat',
        'nama_obat',
        'merk_obat',
        'jenis_obat',
        'stok',
        'harga'
    ];

form pada transaksi obat

<form id="form_transobat" name="form_transobat" class="form-horizontal">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title"id="form_transobat_judul"></h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <input type="hidden" id="id" name="id" >
        <div class="modal-body">
            <div class="form-group">
              <label for="nama_pasien">Kode Rekamedik</label>
              <input type="text" class="form-control" id="kode_rekmed" name="kode_rekmed" placeholder="Masukan Kode Rekamedik" value="{{$row->kode_rekmed}}" required readonly>
            </div>
            <div class="form-group">
              <label for="kode_pegawai">Kode Registrasi</label>
              <input type="text" class="form-control" id="kode_register" name="kode_register" placeholder="Masukan Kode Registrasi" value="{{$row->kode_register}}" required readonly>
            </div>
            <div class="form-group">
              <label for="kode_pasien">Kode Obat</label>
              <select class="form-control select2bs4" style="width: 100%;" id="kd_obat" name="kd_obat">
                <option disabled selected="selected" value="" required>Silahkan Pilih..</option>
                @foreach($obat as $obats)
                  <option value="{{$obats->kd_obat}}" >{{$obats->nama_obat}}</option>
                @endforeach
              </select>
            </div>
            <div class="form-group">
              <label for="harga">Harga</label>
              <select class="form-control select2bs4" style="width: 100%;" id="harga" name="harga">
                <option disabled selected="selected" value="" required>Silahkan Pilih..</option>
                @foreach($obat as $obats)
                  <option value="{{$obats->harga}}" >{{$obats->nama_obat}}-{{$obats->harga}}</option>
                @endforeach
              </select>
            </div>
            <div class="form-group">
              <label for="kuantitas">Jumlah</label>
              <input type="text" class="form-control" id="kuantitas" name="kuantitas" placeholder="Masukan Jumlah Obat" value="" onkeypress="return event.charCode >= 48 && event.charCode <=57" required>
            </div>
        </div>
        <div class="modal-footer justify-content-between">
          <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
          <button type="submit" class="btn btn-primary" id="tombol-simpan" value="create">Simpan</button>
        </div>
      </div>
    </form>
avatar dwigumelar
@dwigumelar

8 Kontribusi 0 Poin

Diperbarui 3 tahun yang lalu

3 Jawaban:

<div>coba pada fungsi store ditambahkan ubah update stok nya</div><pre>$obat = ModelObat::whereKd_obat($request-&gt;kd_obat)-&gt;get();</pre><div><br></div><pre>$obat-&gt;update(['stok'=&gt;$obat-&gt;stok - $request-&gt;kuantitas]);</pre>

avatar sudjitoe
@sudjitoe

21 Kontribusi 14 Poin

Dipost 3 tahun yang lalu

Tanggapan

di tambah di bawah $post?

<div>udah dicoba seperti ini</div><pre> public function store(Request $request) { $id = $request-&gt;id;

  $post   =   Obat_tran::updateOrCreate([&#039;id&#039; =&amp;gt; $id],
              [
                  &#039;kode_rekmed&#039; =&amp;gt; $request-&amp;gt;kode_rekmed,
                  &#039;kode_register&#039; =&amp;gt; $request-&amp;gt;kode_register,
                  &#039;kd_obat&#039; =&amp;gt; $request-&amp;gt;kd_obat,
                  &#039;kuantitas&#039; =&amp;gt; $request-&amp;gt;kuantitas,
                  &#039;subtotal&#039; =&amp;gt; $request-&amp;gt;kuantitas*$request-&amp;gt;harga,
              ]);
  $obat = Obat::wherekd_obat($request-&amp;gt;kd_obat)-&amp;gt;get();
  $obat-&amp;gt;update([&#039;stok&#039;=&amp;gt;$obat-&amp;gt;stok - $request-&amp;gt;kuantitas]);

  return response()-&amp;gt;json($post,$obat);
}&lt;/pre&gt;&lt;div&gt;stoknya tetep ngga update gan :(&lt;/div&gt;
avatar dwigumelar
@dwigumelar

8 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

<div>bisa pake kode ini gan</div><div>makasih atas petunjuknya<br><br></div><pre> $id = $request-&gt;id;

  $post   =   Obat_tran::updateOrCreate([&#039;id&#039; =&amp;gt; $id],
              [
                  &#039;kode_rekmed&#039; =&amp;gt; $request-&amp;gt;kode_rekmed,
                  &#039;kode_register&#039; =&amp;gt; $request-&amp;gt;kode_register,
                  &#039;kd_obat&#039; =&amp;gt; $request-&amp;gt;kd_obat,
                  &#039;kuantitas&#039; =&amp;gt; $request-&amp;gt;kuantitas,
                  &#039;subtotal&#039; =&amp;gt; $request-&amp;gt;kuantitas*$request-&amp;gt;harga,
              ]);
  $where = array(&#039;kd_obat&#039; =&amp;gt; $request-&amp;gt;kd_obat);
  $post  = Obat::where($where)-&amp;gt;first();
  $post-&amp;gt;update([&#039;stok&#039;=&amp;gt;$post-&amp;gt;stok - $request-&amp;gt;kuantitas]);

  return response()-&amp;gt;json($post); &lt;/pre&gt;
avatar dwigumelar
@dwigumelar

8 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban