81 lines
1.9 KiB
PHP
81 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\History;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TranslateController extends Controller
|
|
{
|
|
/**
|
|
* Halaman penerjemah
|
|
*/
|
|
public function index()
|
|
{
|
|
$histories = History::where('user_id', auth()->id())
|
|
->latest()
|
|
->get();
|
|
|
|
return view('translate.index', compact('histories'));
|
|
}
|
|
|
|
/**
|
|
* Simpan hasil terjemahan + gambar
|
|
*/
|
|
public function storeResult(Request $request)
|
|
{
|
|
$request->validate([
|
|
'hasil_teks' => 'required|string',
|
|
'image' => 'nullable|image|mimes:jpg,jpeg,png|max:5120'
|
|
]);
|
|
|
|
$gambar = null;
|
|
|
|
// ==========================
|
|
// Upload gambar
|
|
// ==========================
|
|
|
|
if ($request->hasFile('image')) {
|
|
|
|
$file = $request->file('image');
|
|
|
|
$namaFile = time() . '_history.' . $file->getClientOriginalExtension();
|
|
|
|
$file->move(public_path('uploads'), $namaFile);
|
|
|
|
$gambar = 'uploads/' . $namaFile;
|
|
}
|
|
|
|
// ==========================
|
|
// Simpan ke database
|
|
// ==========================
|
|
|
|
$history = History::create([
|
|
'user_id' => auth()->id(),
|
|
'hasil_teks' => $request->hasil_teks,
|
|
'gambar' => $gambar
|
|
]);
|
|
|
|
// ==========================
|
|
// Return JSON
|
|
// ==========================
|
|
|
|
return response()->json([
|
|
'message' => 'Riwayat berhasil disimpan.',
|
|
'history' => [
|
|
|
|
'id' => $history->id,
|
|
|
|
'hasil_teks' => $history->hasil_teks,
|
|
|
|
'gambar_url' => $gambar
|
|
? asset($gambar)
|
|
: null,
|
|
|
|
'created_at' => $history->created_at->format('d M Y H:i'),
|
|
|
|
'time_ago' => $history->created_at->diffForHumans()
|
|
]
|
|
]);
|
|
}
|
|
} |