<?php
namespace App\Http\Controllers;
use App\Models\cabor;
use App\Models\club;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class CaborController extends Controller
{
public function index()
{
$data['cabor'] = cabor::orderby('status', 'desc')->get();
return view('admin.cabor.index')->with($data);
}
public function store(Request $request)
{
$request->validate([
'nama' => 'required',
'status' => 'required',
'photo' => 'required',
]);
$data = [
'nama' => $request->nama,
'status' => $request->status,
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "logoCabor-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/logocabor'), $foto_baru);
$data['photo'] = $foto_baru;
}
$create = cabor::create($data);
if ($create) {
return redirect()->route('cabors.index')->with('success', 'Data baru telah ditambahkan');
} else {
return redirect()->route('cabors.index')->with('error', 'Data gagal ditambahkan');
}
}
public function update(Request $request, String $id)
{
$decryptedId = Crypt::decryptString($id);
try {
$request->validate([
'nama' => 'required',
'status' => 'required',
]);
$data = [
'nama' => $request->nama,
'status' => $request->status,
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "LogoCabor-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/logocabor'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = cabor::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images/logoCabor') . "/" . $foto_lama);
}
$update = cabor::where('id', $decryptedId)->update($data);
if ($update) {
return redirect()->route('cabors.index')->with('success', 'Data dari telah diedit');
} else {
return redirect()->route('cabors.index')->with('error', 'Data gagal diedit');
}
} catch (DecryptException $e) {
return redirect()->route('cabors.index')->with('error', 'Data gagal diedit');
}
}
public function destroy(String $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$user = cabor::where('id', $decryptedId);
$user->delete();
return redirect()->route('cabors.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]