I want to Import data excel to my database, with rules if 'stok'[row 5] > 2 columns of 'keterangan' will be filled 'Tersedia', else if 'stok'[row 5] !=0 2 columns of 'keterangan' will be filled 'Hampir Habis', else columns of 'keterangan' will be filled 'Habis'. what code should I write to the controller of import data? I'm using laravel version 8
This is the code of import
<?php
public function model(array $row)
{
$product_status = Produk::where('stok', $row[5]);
if ($product_status > 2) {
$ket= new Produk([
'keterangan' => "Tersedia",]);
$ket->save();
} elseif ($product_status != 0) {
$ket= new Produk([
'keterangan' => "Hampir Habis",]);
$ket->save();
} else {
$ket= new Produk([
'keterangan' => "Habis",]);
$ket->save();
}
return new Produk([
'kode_barang' => $row[0],
'jenis_barang' => $row[1],
'nama_barang' => $row[2],
'tipe' => $row[3],
'kategori_id' => $row[4],
'stok' => $row[5],
'harga' => $row[6],
'harga_beli' => $row[7],
]);
}
public function rules(): array
{
return [
'0' => function ($attribute, $value, $onFailure) {
if (Produk::where('kode_barang', '=', $value)->count() > 0) {
$onFailure('tersedia');
} elseif ($value == null || $value == '') {
$onFailure('kosong');
} elseif ($value == 0) {
$onFailure('nol');
}
},
'1' => 'required|string',
'2' => 'required|string',
'5' => 'required|numeric',
'6' => 'required|numeric',
'7' => 'required|numeric',
];
}
}
this is the controller
public function imporproduk(Request $request){
// validasi
$this->validate($request, [
'file' => 'required|mimes:csv,xls,xlsx'
]);
// menangkap file excel
$file = $request->file('file');
// membuat nama file unik
$nama_file = rand().$file->getClientOriginalName();
// upload ke folder file_produk di dalam folder public
$file->move('file_produk/',$nama_file);
// import data
Excel::import(new ProdukImport, 'file_produk/'.$nama_file);
// notifikasi dengan session
Alert::success('sukses','Data produk Berhasil Diimport!');
// alihkan halaman kembali
return redirect()->back();
}
The error said Property [stok] does not exist on the Eloquent builder instance.
Sorry I'm a newbie using laravel 8
question from:
https://stackoverflow.com/questions/65644737/object-of-class-illuminate-database-eloquent-builder-could-not-be-converted-to-i 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…