[Api Template & Kategori]
This commit is contained in:
parent
e4ffa61083
commit
97d79b4a84
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\Kategori;
|
||||||
|
|
||||||
|
class KategoriApiController extends Controller
|
||||||
|
{
|
||||||
|
// Ambil semua kategori
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
// 1. Ambil semua kategori dari database
|
||||||
|
$kategoris = Kategori::all();
|
||||||
|
|
||||||
|
// 2. Ubah koleksi data untuk membuat URL foto yang benar
|
||||||
|
$transformedKategoris = $kategoris->map(function($kategori) {
|
||||||
|
return [
|
||||||
|
'id' => $kategori->id,
|
||||||
|
'nama' => $kategori->nama,
|
||||||
|
'deskripsi' => $kategori->deskripsi,
|
||||||
|
// Gunakan helper asset() untuk membuat URL lengkap
|
||||||
|
'foto' => $kategori->foto ? asset('storage/' . $kategori->foto) : null,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. Kirim data yang sudah diubah sebagai JSON
|
||||||
|
return response()->json($transformedKategoris);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ambil detail satu kategori
|
||||||
|
public function show(Kategori $kategori)
|
||||||
|
{
|
||||||
|
// Sebaiknya detail juga diubah agar konsisten
|
||||||
|
return response()->json([
|
||||||
|
'id' => $kategori->id,
|
||||||
|
'nama' => $kategori->nama,
|
||||||
|
'deskripsi' => $kategori->deskripsi,
|
||||||
|
'foto' => asset('storage/' . $kategori->foto),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\Template;
|
||||||
|
|
||||||
|
class TemplateApiController extends Controller
|
||||||
|
{
|
||||||
|
// Ambil semua template
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$templates = Template::with('kategori')->get();
|
||||||
|
|
||||||
|
$transformedTemplates = $templates->map(function ($template) {
|
||||||
|
return [
|
||||||
|
'id' => $template->id,
|
||||||
|
'nama_template' => $template->nama_template,
|
||||||
|
'harga' => $template->harga,
|
||||||
|
'paket' => $template->paket,
|
||||||
|
'kategori' => $template->kategori ? $template->kategori->nama : null,
|
||||||
|
'foto' => $template->foto ? asset('storage/' . $template->foto) : null,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
return response()->json($transformedTemplates);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ambil detail template tertentu
|
||||||
|
public function show(Template $template)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'id' => $template->id,
|
||||||
|
'nama_template' => $template->nama_template,
|
||||||
|
'harga' => $template->harga,
|
||||||
|
'paket' => $template->paket,
|
||||||
|
'kategori' => $template->kategori ? $template->kategori->nama : null,
|
||||||
|
'foto' => $template->foto ? asset('storage/' . $template->foto) : null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,20 +7,20 @@ use Illuminate\Http\Request;
|
|||||||
|
|
||||||
class TemplateController extends Controller
|
class TemplateController extends Controller
|
||||||
{
|
{
|
||||||
// Lihat semua template (dari seeder)
|
// Lihat semua template (untuk admin web)
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$templates = Template::with('kategori')->get();
|
$templates = Template::with('kategori')->get();
|
||||||
return view('admin.template.index', compact('templates'));
|
return view('admin.template.index', compact('templates'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Form edit (untuk ubah harga / nama dsb)
|
// Form edit (ubah data template)
|
||||||
public function edit(Template $template)
|
public function edit(Template $template)
|
||||||
{
|
{
|
||||||
return view('admin.template.edit', compact('template'));
|
return view('admin.template.edit', compact('template'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update template (ubah harga / nama / paket)
|
// Update template (nama, harga, paket)
|
||||||
public function update(Request $request, Template $template)
|
public function update(Request $request, Template $template)
|
||||||
{
|
{
|
||||||
$validated = $request->validate([
|
$validated = $request->validate([
|
||||||
|
|||||||
@ -26,6 +26,7 @@ class TemplateSeeder extends Seeder
|
|||||||
]
|
]
|
||||||
],
|
],
|
||||||
'kategori_id' => $k1->id,
|
'kategori_id' => $k1->id,
|
||||||
|
'foto' => 'templates/Pernikahan.jpg', // taruh di storage/app/public/templates/
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Template::create([
|
Template::create([
|
||||||
@ -40,6 +41,22 @@ class TemplateSeeder extends Seeder
|
|||||||
]
|
]
|
||||||
],
|
],
|
||||||
'kategori_id' => $k2->id,
|
'kategori_id' => $k2->id,
|
||||||
|
'foto' => 'templates/HBD.jpg',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Template::create([
|
||||||
|
'nama_template' => 'Undangan Khitan Modern',
|
||||||
|
'harga' => 85000,
|
||||||
|
'paket' => 'premium',
|
||||||
|
'form' => [
|
||||||
|
'fields' => [
|
||||||
|
['name' => 'nama_anak', 'label' => 'Nama Anak', 'type' => 'text'],
|
||||||
|
['name' => 'tanggal_acara', 'label' => 'Tanggal Acara', 'type' => 'date'],
|
||||||
|
['name' => 'lokasi', 'label' => 'Lokasi', 'type' => 'text'],
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'kategori_id' => $k3->id,
|
||||||
|
'foto' => 'templates/Khitan.jpg',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,18 +2,13 @@
|
|||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use App\Http\Controllers\Api\TemplateApiController;
|
||||||
|
use App\Http\Controllers\Api\KategoriApiController;
|
||||||
|
|
||||||
|
Route::get('kategoris', [KategoriApiController::class, 'index']);
|
||||||
|
Route::get('kategoris/{kategori}', [KategoriApiController::class, 'show']);
|
||||||
|
|
||||||
|
Route::get('/templates', [TemplateApiController::class, 'index']);
|
||||||
|
Route::get('/templates/{template}', [TemplateApiController::class, 'show']);
|
||||||
|
|
||||||
/*
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| API Routes
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| Here is where you can register API routes for your application. These
|
|
||||||
| routes are loaded by the RouteServiceProvider and all of them will
|
|
||||||
| be assigned to the "api" middleware group. Make something great!
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
|
|
||||||
return $request->user();
|
|
||||||
});
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user