<?php
namespace App\Http\Controllers;
use App\Models\berita;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
class BeritaController extends Controller
{
public function index()
{
$data['berita'] = berita::get();
return view('berita.index')->with($data);
}
public function tambah(){
return view ('berita.add');
}
public function ubah(string $id){
$decryptedId = Crypt::decryptString($id);
$data['berita'] = berita::where('id',$decryptedId)->first();
return view ('berita.edit')->with($data);
}
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'deskripsi' => 'required',
'tag' => 'required',
'photo' => 'mimes:jpg,jpeg,png,gif,svg|required',
]);
$data = [
'tanggal' => now(),
'title' => $request->title,
'tag' => $request->tag,
'banner' => 0,
'deskripsi' => nl2br($request->deskripsi),
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "Berita-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images'), $foto_baru);
$data['photo'] = $foto_baru;
}
berita::create($data);
return redirect()->route('berita.index')->with('success', 'berita berhasil dibuat!');
}
public function update(request $request,string $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$request->validate([
'tanggal' => 'required',
'title' => 'required',
'deskripsi' => 'required',
]);
$data = [
'tanggal' => $request->tanggal,
'title' => $request->title,
'tag' => $request->tag,
'deskripsi' => nl2br($request->deskripsi),
];
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "Berita-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images'), $foto_baru);
$data['photo'] = $foto_baru;
$getfoto = berita::where('id', $decryptedId)->first();
$foto_lama = $getfoto->photo;
File::delete(public_path('images') . "/" . $foto_lama);
}
berita::where('id', $decryptedId)->update($data);
return redirect()->route('berita.index')->with('success', 'Data successfully changed');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
public function destroy(string $id)
{
try {
$decryptedId = Crypt::decryptString($id);
$berita = berita::where('id', $decryptedId);
$foto = $berita->first();
File::delete(public_path('images') . "/" . $foto->photo);
$berita->delete();
return redirect()->route('berita.index')->with('delete', 'Data successfully deleted');
} catch (DecryptException $e) {
return redirect()->back()->with('error', 'Invalid ID');
}
}
public function banner(string $id)
{
$decryptedId = Crypt::decryptString($id);
$data = ['banner' => 1];
berita::where('id', $decryptedId)->update($data);
return redirect()->route('berita.index')->with('success', 'Berita berhasil dipasang kebanner!');
}
public function no_banner(string $id)
{
$decryptedId = Crypt::decryptString($id);
$data = ['banner' => 0];
berita::where('id', $decryptedId)->update($data);
return redirect()->route('berita.index')->with('delete', 'Berita dilepas dari banner!');
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]