Skip to content

Commit

Permalink
Fix: bug on creating data santri
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadMuzayyin committed Feb 18, 2024
1 parent a6a5a34 commit f7032ee
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 117 deletions.
89 changes: 33 additions & 56 deletions app/Http/Controllers/Santri/SantriController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,7 @@ public function store(SantriRequest $request)
$tgl = Carbon::parse($request->tanggal_boyong);
$validate['tanggal_boyong_hijriyah'] = isset($request->tanggal_boyong) ? str_replace('/', '-', $tgl->toHijri()->isoFormat('LL')) : '';
$foto = $request->file('foto');
if (isset($foto) == false) {
$user = User::create([
'name' => $request->nama_lengkap,
'email' => 'santri_' . Str::slug($request->nama_lengkap) . config('app.domain'),
'password' => bcrypt('password'),
'role_id' => 4,
]);
// insert wali santri
$wali = WaliSantri::create([
'nama_ayah' => $validate['nama_ayah'],
'nama_ibu' => $validate['nama_ibu'],
]);
$user->assignRole('Santri');
$validate['user_id'] = $user->id;
$validate['wali_santri_id'] = $wali->id;
$santri = Santri::create($validate);
$alamat = AlamatSantri::create([
'santri_id' => $santri->id,
'provinsi_id' => $validate['provinsi_id'],
'kabupaten_id' => $validate['kabupaten_id'],
'kecamatan_id' => $validate['kecamatan_id'],
'kelurahan_id' => $validate['kelurahan_id'],
'dusun' => $validate['dusun'],
]);
if (!$santri) {
$user->delete();
$wali->delete();
$alamat->delete();
} else {
User::find($validate['user_id'])->assignRole('Santri');
}
} else {
if (isset($foto) == true) {
$path = storage_path('app/public/uploads/santri/');
$filename = $foto->hashName();

Expand All @@ -100,32 +69,40 @@ public function store(SantriRequest $request)
$constraint->upsize();
$constraint->aspectRatio();
})->save($path . $filename);

// insert user login santri
$user = User::create([
'name' => $request->nama_lengkap,
'email' => 'santri_' . Str::slug($request->nama_lengkap) . config('app.domain'),
'password' => bcrypt('password'),
]);
// insert wali santri
$wali = WaliSantri::create([
'nama_ayah' => $validate['nama_ayah'],
'nama_ibu' => $validate['nama_ibu'],
]);

// insert santri
$validate['user_id'] = $user->id;
$validate['foto'] = $filename;
$validate['wali_santri_id'] = $wali->id;
$santri = Santri::create($validate);
if (!$santri) {
$user->delete();
$wali->delete();
} else {
User::find($validate['user_id'])->assignRole('Santri');
}
} else {
$validate['foto'] = 'santri.png';
}
$user = User::create([
'name' => $request->nama_lengkap,
'email' => 'santri_' . Str::slug($request->nama_lengkap) . config('app.domain'),
'password' => bcrypt('password'),
'role_id' => 4,
]);
// insert wali santri
$wali = WaliSantri::create([
'nama_ayah' => $validate['nama_ayah'],
'nama_ibu' => $validate['nama_ibu'],
]);
$user->assignRole('Santri');
$validate['user_id'] = $user->id;
$validate['wali_santri_id'] = $wali->id;
$santri = Santri::create($validate);
$alamat = AlamatSantri::create([
'santri_id' => $santri->id,
'provinsi_id' => $validate['provinsi_id'],
'kabupaten_id' => $validate['kabupaten_id'],
'kecamatan_id' => $validate['kecamatan_id'],
'kelurahan_id' => $validate['kelurahan_id'],
'dusun' => $validate['dusun'],
]);
if (!$santri) {
$user->delete();
$wali->delete();
$alamat->delete();
} else {
User::find($validate['user_id'])->assignRole('Santri');
}

// update kamar
if (!$request->tanggal_boyong) {
$kamar = Kamar::where('id', $validate['kamar_id'])->first();
Expand Down
122 changes: 61 additions & 61 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ public function run(): void
{
// \App\Models\User::factory(50)->create();

// \App\Models\User::create([
// 'name' => 'Administrator',
// 'email' => 'admin' . config('app.domain'),
// 'password' => bcrypt('password'),
// ]);
// \App\Models\User::create([
// 'name' => 'Operator Tabungan',
// 'email' => 'keuangan' . config('app.domain'),
// 'password' => bcrypt('password'),
// ]);
// \App\Models\User::create([
// 'name' => 'Pengurus Pondok',
// 'email' => 'pengurus' . config('app.domain'),
// 'password' => bcrypt('password'),
// ]);
\App\Models\User::create([
'name' => 'Administrator',
'email' => 'admin' . config('app.domain'),
'password' => bcrypt('password'),
]);
\App\Models\User::create([
'name' => 'Operator Tabungan',
'email' => 'keuangan' . config('app.domain'),
'password' => bcrypt('password'),
]);
\App\Models\User::create([
'name' => 'Pengurus Pondok',
'email' => 'pengurus' . config('app.domain'),
'password' => bcrypt('password'),
]);

// $kelas = \App\Models\Kelas::create([
// 'kode' => 'KGJWF31042',
// 'tingkatan' => 'ALFIYAH',
// 'kelas' => 'ALFIYAH SATU',
// 'keterangan' => 'Asuhan K. Zain Fairuz',
// ]);
// \App\Models\Kamar::create([
// 'kode' => 'OKRGX22240',
// 'nama' => 'As Syafii',
// 'blok' => 'A',
// ]);
// $kamar = \App\Models\Kamar::create([
// 'kode' => 'OLKJS33440',
// 'nama' => 'Anwarul Qulubi',
// 'blok' => 'A',
// ]);
$kelas = \App\Models\Kelas::create([
'kode' => 'KGJWF31042',
'tingkatan' => 'ALFIYAH',
'kelas' => 'ALFIYAH SATU',
'keterangan' => 'Asuhan K. Zain Fairuz',
]);
\App\Models\Kamar::create([
'kode' => 'OKRGX22240',
'nama' => 'As Syafii',
'blok' => 'A',
]);
$kamar = \App\Models\Kamar::create([
'kode' => 'OLKJS33440',
'nama' => 'Anwarul Qulubi',
'blok' => 'A',
]);

// sample data
// $user = \App\Models\User::create([
Expand Down Expand Up @@ -81,45 +81,45 @@ public function run(): void
// 'status' => 'Santri Aktif',
// ]);

// $this->call(RoleSeeder::class);
$this->call(RoleSeeder::class);

$jsonFile = File::get(public_path('wilayah/provinsi.json'));
$data = json_decode($jsonFile);

// Iterasi data JSON dan simpan ke database
foreach ($data as $item) {
$jsonFile = File::get(public_path("wilayah/kabupaten/$item->id.json"));
$kabupaten = json_decode($jsonFile);
// foreach ($data as $item) {
// $jsonFile = File::get(public_path("wilayah/kabupaten/$item->id.json"));
// $kabupaten = json_decode($jsonFile);

$pr = Provinsi::create([
'name' => ucwords(strtolower($item->nama)),
]);
foreach ($kabupaten as $kb) {
$jsonFile = File::get(public_path("wilayah/kecamatan/$kb->id.json"));
$kecamatan = json_decode($jsonFile);
// $pr = Provinsi::create([
// 'name' => ucwords(strtolower($item->nama)),
// ]);
// foreach ($kabupaten as $kb) {
// $jsonFile = File::get(public_path("wilayah/kecamatan/$kb->id.json"));
// $kecamatan = json_decode($jsonFile);

$kbptn = Kabupaten::create([
'provinsi_id' => $pr->id,
'name' => ucwords(strtolower($kb->nama)),
]);
// $kbptn = Kabupaten::create([
// 'provinsi_id' => $pr->id,
// 'name' => ucwords(strtolower($kb->nama)),
// ]);

foreach ($kecamatan as $kc) {
$jsonFile = File::get(public_path("wilayah/kelurahan/$kc->id.json"));
$kelurahan = json_decode($jsonFile);
// foreach ($kecamatan as $kc) {
// $jsonFile = File::get(public_path("wilayah/kelurahan/$kc->id.json"));
// $kelurahan = json_decode($jsonFile);

$kcm = Kecamatan::create([
'kabupaten_id' => $kbptn->id,
'name' => ucwords(strtolower($kc->nama)),
]);
// $kcm = Kecamatan::create([
// 'kabupaten_id' => $kbptn->id,
// 'name' => ucwords(strtolower($kc->nama)),
// ]);

foreach ($kelurahan as $kl) {
Kelurahan::create([
'kecamatan_id' => $kcm->id,
'name' => ucwords(strtolower($kl->nama)),
]);
}
}
}
}
// foreach ($kelurahan as $kl) {
// Kelurahan::create([
// 'kecamatan_id' => $kcm->id,
// 'name' => ucwords(strtolower($kl->nama)),
// ]);
// }
// }
// }
// }
}
}

0 comments on commit f7032ee

Please sign in to comment.