i have a search feature like this, when you click on the row, it will added to the list.
i’ve been trying to solve this about 3 days now. so i have the same version of the app in my local and my hosting. but i don’t know why in the hosting will show error like this
while in my local works just fine
here is my code
on the resources/views/livewire/kasir/query-barang.blade.php
<div class="table-row cursor-pointer hover:bg-slate-700 hover:text-white"
wire:click="submit({{$br->barcode}})">
submit function on app/Http/Livewire/Kasir/QueryBarang.php
public function submit($barcode)
{
$this->emit('selectBarang', $barcode);
$this->closeModal();
$this->reset();
}
and then the emit will listen by app/Http/Livewire/Kasir/SearchBarang.php
protected $listeners = [
'selectBarang' => 'selectBarang'
];
public function selectBarang($barcode)
{
$this->barcode = $barcode;
$this->submit();
}
public function submit()
{
/* Mengecek apakah barcode berbentuk numeric */
if (is_numeric($this->barcode)) {
/* jika true, maka akan menambahkan barang */
$this->barang = Barang::where('barcode', $this->barcode)->first();
$this->createOrUpdate();
} else {
/* Jika False maka akan mencari dan menampilkan Modal */
$this->queryBarang($this->barcode);
$this->reset('barcode');
}
}
public function createOrUpdate()
{
$checkBarangDalamList = TransaksiJualDetail::where('transaksi_jual_id', $this->transaksiJual)->where('barang_id', $this->barang->id)->first();
if ($checkBarangDalamList == null) {
$this->addNewStuff();
$this->addTotal();
$this->reset('barcode');
$this->emit('storeNewList', $this->transaksiJual);
} else {
$this->addQuantity();
$this->addTotal();
$this->reset('barcode');
$this->emit('storeNewList', $this->transaksiJual);
}
}
in the error as shown above, it clearly say that $this->barang
is null
. when i try to diedump $this->barcode
on the submit function, it showing clearly what is the barcode
but, when i try to diedump $this->barang
on the submit function it will showing me null
value
What is wrong with my code ?
Thanks for the answer
2
Answers
after trying to fix this for 3 days, finally i got something that work. so i have to change the query from this
to this
if you asking me what is the different, tbh i don't know. and i don't know either why it works.
Hypothesis: Two of your statements make me suspicious that it’s a data issue, not code.
In the submit function where
$this->barcode
has a value but$this->barang
is null. The only operation is database query.$this->barang = Barang::where(‘barcode’, $this->barcode)->first();
The issue happens in HOSTING while working on LOCAL. I assume your HOSTING environment and LOCAL environment use different databases.
Propose Actions: Please check whether the data is available in HOSTING as same as LOCAL.
You can use firstOrFail() collection instead of first() to detect and handle this error.