Postingan lainnya
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">×</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>
0
3 Jawaban:
<div>coba pada fungsi store ditambahkan ubah update stok nya</div><pre>$obat = ModelObat::whereKd_obat($request->kd_obat)->get();</pre><div><br></div><pre>$obat->update(['stok'=>$obat->stok - $request->kuantitas]);</pre>
0
Tanggapan
di tambah di bawah $post?
<div>udah dicoba seperti ini</div><pre> public function store(Request $request) { $id = $request->id;
$post = Obat_tran::updateOrCreate(['id' =&gt; $id],
[
'kode_rekmed' =&gt; $request-&gt;kode_rekmed,
'kode_register' =&gt; $request-&gt;kode_register,
'kd_obat' =&gt; $request-&gt;kd_obat,
'kuantitas' =&gt; $request-&gt;kuantitas,
'subtotal' =&gt; $request-&gt;kuantitas*$request-&gt;harga,
]);
$obat = Obat::wherekd_obat($request-&gt;kd_obat)-&gt;get();
$obat-&gt;update(['stok'=&gt;$obat-&gt;stok - $request-&gt;kuantitas]);
return response()-&gt;json($post,$obat);
}</pre><div>stoknya tetep ngga update gan :(</div>
0
<div>bisa pake kode ini gan</div><div>makasih atas petunjuknya<br><br></div><pre> $id = $request->id;
$post = Obat_tran::updateOrCreate(['id' =&gt; $id],
[
'kode_rekmed' =&gt; $request-&gt;kode_rekmed,
'kode_register' =&gt; $request-&gt;kode_register,
'kd_obat' =&gt; $request-&gt;kd_obat,
'kuantitas' =&gt; $request-&gt;kuantitas,
'subtotal' =&gt; $request-&gt;kuantitas*$request-&gt;harga,
]);
$where = array('kd_obat' =&gt; $request-&gt;kd_obat);
$post = Obat::where($where)-&gt;first();
$post-&gt;update(['stok'=&gt;$post-&gt;stok - $request-&gt;kuantitas]);
return response()-&gt;json($post); </pre>
0