mercredi 19 mai 2021

Problem import excel file and save into a table with foreign keys

Code :

class BarangImport implements ToModel, WithHeadingRow, WithValidation { use Importable;

/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
    $row['kategori'] = Kategori::where('nama_kategori', 'like', '%'.$row['kategori'].'%'); //tabel kategori 

    return new Barang([            

        'kode_barang' => $row['kode'],
        'nama_barang' => $row['nama'],
        'stok' => $row['stok'],
        'satuan' => $row['satuan'],
        'stok_minimal' => $row['stok_minimal'],
        'id_kategori' => $row['kategori'],
        'keterangan' => $row['keterangan'],
        'status' => $row['status'],
    ]);
}

public function rules(): array{
    return [
        'kode' => 'required|unique:barang,kode_barang',
        'nama'=>'required|unique:barang,nama_barang',
        'stok' =>'required|numeric',
        'stok_minimal' =>'required|numeric',
        'status' => 'required|in:aktif,tidak aktif',
        'kategori' => 'required',
        
    ];
}

public function customValidationMessages()
{
    return [
        'kode.unique' => 'Kode barang sudah digunakan',
        'nama.unique'=> 'Nama barang sudah digunakan',
        'kode.required' => 'Kolom kode masih kosong',
        'nama.required' => 'Kolom nama barang masih kosong',
        'stok.required' => 'Kolom stok masih kosong',
        'stok.numeric' => 'Kolom stok harus berupa angka',
        'stok_minimal.required' => 'Kolom stok minimal masih kosong',
        'stok_minimal.numeric' => 'Kolom stok minimal harus berupa angka',
        'status.required' => 'Kolom status masih kosong',
        'status.in' => 'Isian status tidak valid',
        'kategori'=> 'Kolom kategori masih kosong',
        
    ];
}

}

This is the file that will be imported

enter image description here

I want to import file with foreign key category_name of category table, but I am confused. Because there are many statements. You can see the query above.

How and what is wrong, please help?enter code here



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3f2o972
via IFTTT

Aucun commentaire:

Enregistrer un commentaire