<?php
namespace App\Http\Controllers;
use App\Models\banner;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class BannerController extends Controller
{
public function index()
{
$data['banner'] = banner::get();
return view('admin.banner.index')->with($data);
}
public function store(Request $request)
{
$request->validate([
'photo' => 'required|mimes:jpg,jpeg,png|max:300000',
], ['photo.max' => 'Foto yang tidak boleh diatas 300mb!']);
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "banner-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/banner'), $foto_baru);
$data['photo'] = $foto_baru;
}
$create = banner::create($data);
if ($create) {
return redirect()->route('banners.index')->with('success', 'Data baru telah ditambahkan');
} else {
return redirect()->route('banners.index')->with('error', 'Data gagal ditambahkan');
}
}
public function update(Request $request, String $id)
{
$decryptedId = Crypt::decryptString($id);
try {
$request->validate([
'photo' => 'mimes:jpg,jpeg,png|max:300000',
], ['photo.max' => 'Foto yang tidak boleh diatas 300mb!']);
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "banner-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/banner'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = banner::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images/banner') . "/" . $foto_lama);
}
$update = banner::where('id', $decryptedId)->update($data);
if ($update) {
return redirect()->route('banners.index')->with('success', 'Data dari telah diedit');
} else {
return redirect()->route('banners.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);
$banner = banner::where('id', $decryptedId);
$foto = $banner->first();
File::delete(public_path('images/banner') . "/" . $foto->photo);
$banner->delete();
return redirect()->route('banners.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]