57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Recommendation;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminRekomendasiController extends Controller
|
|
{
|
|
/**
|
|
* Helper function untuk mengekstrak ID video dari URL YouTube.
|
|
*/
|
|
private function extractYouTubeId(string $url): ?string
|
|
{
|
|
preg_match('/(v=|vi=|youtu.be\/|embed\/|\/v\/|\?v=|\&v=)(.+?)\b/i', $url, $matches);
|
|
return $matches[2] ?? null;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$rekomendasiMentah = Recommendation::latest()->get();
|
|
|
|
// Menambahkan thumbnail YouTube ke setiap rekomendasi
|
|
$semuaRekomendasi = $rekomendasiMentah->map(function ($item) {
|
|
$videoId = $this->extractYouTubeId($item->youtube_link);
|
|
return [
|
|
'id' => $item->id,
|
|
'judul' => $item->judul,
|
|
'kategori' => $item->kategori,
|
|
'youtube_link' => $item->youtube_link,
|
|
'thumbnail' => $videoId ? "https://img.youtube.com/vi/{$videoId}/hqdefault.jpg" : 'https://via.placeholder.com/150?text=No+Preview',
|
|
'deskripsi' => $item->deskripsi,
|
|
];
|
|
});
|
|
|
|
return view('admin.rekomendasi.index', [
|
|
'pageTitle' => 'Manajemen Rekomendasi',
|
|
'semuaRekomendasi' => $semuaRekomendasi
|
|
]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('admin.rekomendasi.create', ['pageTitle' => 'Tambah Rekomendasi']);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$rekomendasi = Recommendation::findOrFail($id);
|
|
|
|
return view('admin.rekomendasi.edit', [
|
|
'pageTitle' => 'Edit Rekomendasi: ' . $rekomendasi->judul,
|
|
'rekomendasi' => $rekomendasi
|
|
]);
|
|
}
|
|
} |