Update
This commit is contained in:
@@ -1234,6 +1234,16 @@ class DokterController extends Controller
|
||||
$pesan = $e->getMessage();
|
||||
}
|
||||
return $pesan;
|
||||
} else if ($worklist == 'arsipkan'){
|
||||
try {
|
||||
Periksa::where('created_at', 'LIKE', $request->input('isi').'%')->update([
|
||||
'status'=> 'Arsip'
|
||||
]);
|
||||
$pesan = 'Sejumlah data di rentang '.$request->input('isi').' Berhasil di arsipkan';
|
||||
} catch (Exception $e) {
|
||||
$pesan = $e->getMessage();
|
||||
}
|
||||
return $pesan;
|
||||
} else if ($worklist == 'getvitek'){
|
||||
$isidata = $request->input('isi');
|
||||
$getdataawal = Periksa::where('nofoto', $nofoto)->first();
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
use ZipArchive;
|
||||
|
||||
class JsonTransferController extends Controller
|
||||
{
|
||||
protected $exportPath = 'database/seeders/data/';
|
||||
|
||||
public function index()
|
||||
{
|
||||
$tables = DB::select("SELECT tablename FROM pg_tables WHERE schemaname = 'public'");
|
||||
$tableNames = array_map(fn($t) => $t->tablename, $tables);
|
||||
|
||||
return view('backup', compact('tableNames'));
|
||||
}
|
||||
|
||||
public function generate()
|
||||
{
|
||||
$tables = DB::select("SELECT tablename FROM pg_tables WHERE schemaname = 'public'");
|
||||
$tableNames = array_map(fn($t) => $t->tablename, $tables);
|
||||
|
||||
foreach ($tableNames as $table) {
|
||||
try {
|
||||
$data = DB::table($table)->get();
|
||||
$json = json_encode($data, JSON_PRETTY_PRINT);
|
||||
file_put_contents(base_path($this->exportPath . $table . '.json'), $json);
|
||||
} catch (\Exception $e) {
|
||||
// Bisa log error-nya atau skip tabel ini
|
||||
\Log::warning("Gagal ekspor tabel $table: " . $e->getMessage());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', 'Semua tabel berhasil diekspor.');
|
||||
}
|
||||
|
||||
public function downloadTable($table)
|
||||
{
|
||||
$file = base_path($this->exportPath . $table . '.json');
|
||||
|
||||
if (!file_exists($file)) {
|
||||
return abort(404, 'File tidak ditemukan.');
|
||||
}
|
||||
|
||||
return response()->download($file);
|
||||
}
|
||||
|
||||
public function downloadAll()
|
||||
{
|
||||
$zipFile = base_path('database/seeders/data/all_json.zip');
|
||||
$zip = new ZipArchive;
|
||||
|
||||
File::delete($zipFile);
|
||||
|
||||
if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) {
|
||||
foreach (File::files(base_path($this->exportPath)) as $file) {
|
||||
$zip->addFile($file->getRealPath(), basename($file));
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
|
||||
return response()->download($zipFile);
|
||||
}
|
||||
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.");
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
// Kosongkan tabel sebelum import (opsional, hati-hati!)
|
||||
DB::table($table)->truncate();
|
||||
|
||||
DB::table($table)->insert($data);
|
||||
|
||||
DB::commit();
|
||||
return redirect()->back()->with('success', "Data untuk tabel $table berhasil di-import.");
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with('error', "Gagal import tabel $table: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user