I have a problem with my script
This is My Code.
1. DataIndustri Migration
public function up()
{
Schema::create('data_industri', function (Blueprint $table) {
$table->id();
$table->string('nama_perusahaan')->nullable();
$table->string('pemilik_perusahaan')->nullable();
$table->string('kategori_id')->nullable();
$table->string('alamat_perusahaan')->nullable();
$table->string('kecamatan_id')->nullable();
$table->integer('klbi')->nullable();
$table->string('slug')->nullable();
$table->string('gambar')->nullable();
$table->integer('nilai_investasi')->nullable();
$table->integer('jml_tenaga_kerja')->nullable();
$table->timestamps();
});
}
2. DataIndustriModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class DataIndustriModel extends Model
{
use HasFactory;
protected $table = 'data_industri';
protected $fillable = [
'nama_perusahaan',
'pemilik_perusahaan',
'alamat_perusahaan',
'kategori_id',
'kecamatan_id',
'nilai_investasi',
'jml_tenaga_kerja',
'koordinat',
'slug',
'gambar',
];
public function kategoris()
{
return $this->belongsTo(KategoriIndustriModel::class, 'kategori_id', 'id');
}
public function kecamatans()
{
return $this->belongsTo(KecamatanModel::class, 'kecamatan_id', 'id');
}
public function produk_industris()
{
return $this->hasMany(ProdukIndustriModel::class, 'nama_perusahaan_id', 'id' );
}
}
- produk_industri migration
public function up()
{
Schema::create('produk_industri', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('nama_perusahaan_id')->nullable();
$table->foreign('nama_perusahaan_id')->references('id')
->on('data_industri');
$table->string('komoditi')->nullable();
$table->integer('kapasitas_komoditi')->nullable();
$table->string('satuan_komoditi')->nullable();
$table->timestamps();
});
}
4. DataProdukIndustriModel
class ProdukIndustriModel extends Model
{
use HasFactory;
protected $fillable = [
'nama_perusahaan_id',
'produk_industri_id',
'komoditi',
'kapasitas_komoditi',
'satuan_komoditi',
];
}
and this is my controller
5. StoreController
public function store(Request $request)
{
$this->validate($request, [
'nama_perusahaan' => 'required',
'pemilik_perusahaan' => 'required',
'alamat' => 'required',
'kecamatan' => 'required',
'kategori' => 'required',
'nilai_investasi' => 'required',
'jml_tenaga_kerja' => 'required',
'koordinat' => 'required',
'klbi' => 'required',
'komoditi' => 'required',
'satuan_komoditi' => 'required',
'kapasitas_komoditi' => 'required',
'gambar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
],[
'nama_perusahaan.required' => 'Nama Perusahaan Mohon Diisi',
'pemilik_perusahaan.required' => 'Pemilik Perusahaan Mohon Diisi',
'alamat.required' => 'Alamat Perusahaan Mohon Diisi',
'kecamatan.required' => 'Kecematan Mohon Dipilih',
'kategori.required' => 'Kategori Perusahaan Mohon Dipilih',
'nilai_investasi.required' => 'Nilai Investasi Mohon Diisi',
'jml_tenaga_kerja.required' => 'Jumlah Tenaga Kerja Mohon Diisi',
'koordinat.required' => 'Lokasi Koordinat Mohon Diisi',
'klbi.required' => 'Kode KLBI Mohon Diisi',
'komoditi.required' => 'Jenis Komoditas Belum Diisi',
'satuan_komoditi.required' => 'Satuan Komoditas Belum Diisi',
'kapasitas_komoditi.required' => 'Kapasitas Komoditas Belum Diisi',
'gambar.required' => 'Ukuran Maksimal Gambar 2MB dan Format JPEG, JPG, PNG, GIF'
]
);
$data1 = $request->all();
$data1['nama_perusahaan'] = $request->nama_perusahaan;
$data1['slug'] = Str::slug($request->nama_perusahaan);
$data1['pemilik_perusahaan'] = $request->pemilik_perusahaan;
$data1['alamat_perusahaan'] = $request->alamat;
$data1['kecamatan_id'] = $request->kecamatan;
$data1['kategori_id'] = $request->kategori;
$data1['nilai_investasi'] = $request->nilai_investasi;
$data1['jml_tenaga_kerja'] = $request->jml_tenaga_kerja;
$data1['koordinat'] = $request->koordinat;
$data1['gambar'] = $request->file('gambar')->store('industri','public');
$data2 = $request->all();
$data2['nama_perusahaan_id'] = {{ ????????? }}
$data2['klbi'] = $request->klbi;
$data2['komoditi'] = $request->komoditi;
$data2['satuan_komoditi'] = $request->satuan_komoditi;
$data2['kapasitas_komoditi'] = $request->kapasitas_komoditi;
DataIndustriModel::create($data1);
ProdukIndustriModel::create($data2);
FacadesAlert::success('Berhasil', 'Data Berhasil Tersimpan');
return redirect()->route('index');
}
My question is, how to store nama_perusahaan_id as foreign key in Tabel produk_industri where the value is from id in tabel data_industri
Thanks in advance ..
i hope someone could help me with my script
CodePudding user response:
maybe you should store DataIndustriModel::create($data1) to a variable and after that you can get the nama_perusahaan_id.
$data1 = $request->all();
$data1["nama_perusahaan"] = $request->nama_perusahaan;
$data1["slug"] = Str::slug($request->nama_perusahaan);
$data1["pemilik_perusahaan"] = $request->pemilik_perusahaan;
$data1["alamat_perusahaan"] = $request->alamat;
$data1["kecamatan_id"] = $request->kecamatan;
$data1["kategori_id"] = $request->kategori;
$data1["nilai_investasi"] = $request->nilai_investasi;
$data1["jml_tenaga_kerja"] = $request->jml_tenaga_kerja;
$data1["koordinat"] = $request->koordinat;
$data1["gambar"] = $request->file("gambar")->store("industri", "public");
$data1 = DataIndustriModel::create($data1);
After that you can get nama_perusahaan_id using $data1->id
$data2 = $request->all();
$data2['nama_perusahaan_id'] = $data1->id;
$data2['klbi'] = $request->klbi;
$data2['komoditi'] = $request->komoditi;
$data2['satuan_komoditi'] = $request->satuan_komoditi;
$data2['kapasitas_komoditi'] = $request->kapasitas_komoditi;
ProdukIndustriModel::create($data2);
You can read the documentation from docs