<?php
namespace App\Http\Controllers;
use App\Models\galeri;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class GaleriController extends Controller
{
public function index()
{
$data['galeri'] = galeri::latest()->get();
return view('admin.galeri.index')->with($data);
}
public function store(Request $request)
{
$request->validate([
'kategori' => 'required',
'deskripsi' => 'required',
'photo.*' => 'required|mimes:jpg,jpeg,png|max:300000',
], ['photo.*.max' => 'Foto tidak boleh diatas 300mb!']);
if ($request->hasFile('photo')) {
foreach ($request->file('photo') as $foto_file) {
$foto_ekstensi = $foto_file->extension();
$foto_baru = "galeri-" . uniqid() . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/galeri'), $foto_baru);
$data = [
'kategori' => $request->kategori,
'deskripsi' => $request->deskripsi,
'pengirim' => Auth()->user()->username,
'photo' => $foto_baru, // Assign the photo for this iteration
];
Galeri::create($data);
}
return redirect()->route('galeris.index')->with('success', 'Data baru telah ditambahkan');
}
return redirect()->route('galeris.index')->with('error', 'Data gagal ditambahkan');
}
public function update(Request $request, String $id)
{
$decryptedId = Crypt::decryptString($id);
try {
$request->validate([
'kategori' => 'required',
'deskripsi' => 'required',
'photo' => 'mimes:jpg,jpeg,png|max:300000',
], ['photo.max' => 'Foto yang tidak boleh diatas 300mb!']);
$data = [
'kategori' => $request->kategori,
'deskripsi' => $request->deskripsi,
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "galeri-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/galeri'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = galeri::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images/galeri') . "/" . $foto_lama);
}
$update = galeri::where('id', $decryptedId)->update($data);
if ($update) {
return redirect()->route('galeris.index')->with('success', 'Data dari telah diedit');
} else {
return redirect()->route('galeris.index')->with('error', 'Data gagal diedit');
}
} catch (DecryptException $e) {
return redirect()->route('galeris.index')->with('error', 'Data gagal diedit');
}
}
public function destroy(String $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$user = galeri::where('id', $decryptedId);
$foto = $user->first();
File::delete(public_path('images/galeri') . "/" . $foto->photo);
$user->delete();
return redirect()->route('galeris.index')->with('delete', 'Data berhasil dihapus!');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]