update
This commit is contained in:
@@ -6,7 +6,7 @@ use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use ZipArchive;
|
||||
|
||||
use JsonMachine\Items;
|
||||
class JsonTransferController extends Controller
|
||||
{
|
||||
protected $exportPath = 'database/seeders/data/';
|
||||
@@ -94,30 +94,41 @@ class JsonTransferController extends Controller
|
||||
|
||||
return response()->download($zipFile);
|
||||
}
|
||||
public function importTable($table){
|
||||
public function importTable($table) {
|
||||
$path = base_path($this->exportPath . $table . '.json');
|
||||
if (!file_exists($path)) {
|
||||
return redirect()->back()->with('error', "File JSON untuk tabel $table tidak ditemukan.");
|
||||
}
|
||||
$json = file_get_contents($path);
|
||||
$data = json_decode($json, true);
|
||||
if (!is_array($data)) {
|
||||
return redirect()->back()->with('error', "File JSON untuk tabel $table tidak valid.");
|
||||
return back()->with('error', "File JSON untuk tabel $table tidak ditemukan.");
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
DB::table($table)->truncate();
|
||||
$chunks = array_chunk($data, 200);
|
||||
foreach ($chunks as $chunk) {
|
||||
|
||||
$items = Items::fromFile($path);
|
||||
|
||||
$chunk = [];
|
||||
foreach ($items as $row) {
|
||||
$row = json_decode(json_encode($row), true);
|
||||
$chunk[] = $row;
|
||||
|
||||
if (count($chunk) >= 200) {
|
||||
DB::table($table)->insert($chunk);
|
||||
$chunk = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($chunk)) {
|
||||
DB::table($table)->insert($chunk);
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
return redirect()->back()->with('success', "Data untuk tabel $table berhasil di-import.");
|
||||
return back()->with('success', "Import berhasil!");
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with('error', "Gagal import tabel $table: " . $e->getMessage());
|
||||
return back()->with('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function pull(Request $request){
|
||||
if (session('previlage') !== 'developer') {
|
||||
abort(403, 'Akses ditolak. Anda tidak memiliki izin untuk menjalankan git pull.');
|
||||
|
||||
Reference in New Issue
Block a user