91 lines
2.5 KiB
PHP
91 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Template;
|
|
|
|
class TemplateApiController extends Controller
|
|
{
|
|
// User hanya bisa lihat semua template
|
|
public function index()
|
|
{
|
|
$templates = Template::with(['kategori','fiturs'])
|
|
->get()
|
|
->map(function($t){
|
|
return [
|
|
'id' => $t->id,
|
|
'nama' => $t->nama_template,
|
|
'harga' => (float) $t->harga,
|
|
'foto' => asset('storage/' . $t->foto),
|
|
'kategori' => $t->kategori,
|
|
'fiturs' => $t->fiturs,
|
|
];
|
|
});
|
|
|
|
return response()->json($templates);
|
|
}
|
|
|
|
// User bisa lihat detail 1 template
|
|
public function show(Template $template)
|
|
{
|
|
// UBAH DI SINI: 'fitur' -> 'fiturs'
|
|
return response()->json($template->load(['kategori','fiturs']));
|
|
}
|
|
|
|
|
|
|
|
|
|
public function byCategory($id)
|
|
{
|
|
// UBAH DI SINI: 'fitur' -> 'fiturs'
|
|
$templates = Template::with(['kategori','fiturs'])
|
|
->where('kategori_id', (int)$id)
|
|
->get()
|
|
->map(function($t){
|
|
return [
|
|
'id' => $t->id,
|
|
'nama' => $t->nama_template,
|
|
'harga' => (float) $t->harga,
|
|
'foto' => asset('storage/' . $t->foto),
|
|
'kategori' => $t->kategori,
|
|
// UBAH DI SINI JUGA: $t->fitur -> $t->fiturs
|
|
'fitur' => $t->fiturs,
|
|
];
|
|
});
|
|
|
|
return response()->json($templates);
|
|
}
|
|
|
|
|
|
public function random()
|
|
{
|
|
try {
|
|
// Coba tanpa relationship dulu untuk debug
|
|
$templates = Template::inRandomOrder()
|
|
->take(8)
|
|
->get()
|
|
->map(function($t){
|
|
return [
|
|
'id' => $t->id,
|
|
'nama' => $t->nama_template,
|
|
'harga' => (float) $t->harga,
|
|
'foto' => asset('storage/' . $t->foto),
|
|
'kategori' => $t->kategori,
|
|
'fiturs' => $t->fiturs,
|
|
];
|
|
});
|
|
|
|
return response()->json($templates);
|
|
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'error' => $e->getMessage(),
|
|
'line' => $e->getLine(),
|
|
'file' => $e->getFile()
|
|
], 500);
|
|
}
|
|
|
|
}
|
|
}
|