<?php
namespace App\Http\Livewire;
use App\Models\Barang;
use Livewire\Component;
use Livewire\WithPagination;
class Barangs extends Component
{
use WithPagination;
public $searchTerms;
//nama field
public $nama_barang, $merek_barang, $jenis_barang,$keterangan_barang,$uuid;
//0 = tambah, 1 = edit
public $action = 0;
//keterangan halaman
public $title = "Barang";
public $link = "/barang";
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function render()
{
//$this->barangs = Barang::paginate(10);
$searchTerms = '%'.$this->searchTerms.'%';
return view('livewire.barang.barangs',[
'barangs' => Barang::where('nama_barang','LIKE',$searchTerms)
->orWhere('merek_barang','LIKE',$searchTerms)
->orWhere('jenis_barang','LIKE',$searchTerms)
->orWhere('keterangan_barang','LIKE',$searchTerms)
->paginate(10),
]);
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function create()
{
$this->resetInputFields();
$this->action = 0;
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function resetInputFields(){
$this->uuid = '';
$this->nama_barang = '';
$this->merek_barang = '';
$this->jenis_barang = '';
$this->keterangan_barang = '';
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function store()
{
$this->validate([
'nama_barang' => 'required',
'merek_barang'=>'required',
'jenis_barang' => 'required'
]);
try {
$data = Barang::updateOrCreate(['id' => $this->uuid], [
'nama_barang' => $this->nama_barang,
'merek_barang'=>$this->merek_barang,
'jenis_barang' => $this->jenis_barang,
'keterangan_barang'=>$this->keterangan_barang
]);
if($this->action == 0){
\Helper::addLog("Menambah Barang ".$data->nama_barang);
} else {
\Helper::addLog("Mengubah Barang ".$data->nama_barang);
}
session()->flash('success',
$this->id ? $this->nama_barang.' berhasil di simpan' : $this->nama_barang.' berhasil di simpan');
if($this->action == 0){
$this->resetInputFields();
}
} catch (\Throwable $th) {
session()->flash('error', 'data gagal di simpan');
$this->emit('dataStore',false);
}
$this->emit('dataStore',true);
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function edit($uuid)
{
$barang = Barang::findOrFail($uuid);
$this->uuid = $uuid;
$this->nama_barang = $barang->nama_barang;
$this->merek_barang = $barang->merek_barang;
$this->jenis_barang = $barang->jenis_barang;
$this->keterangan_barang = $barang->keterangan_barang;
$this->action = 1;
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function delete($uuid)
{
$data = Barang::find($uuid);
$data->delete();
\Helper::addLog("Menghapus Barang ".$data->nama_barang);
session()->flash('message', 'barang Deleted Successfully.');
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]