TIF_E41210178/app/Http/Controllers/Api/HewanController.php

59 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Hewan;
use Illuminate\Http\Request;
class HewanController extends Controller
{
public function getHewan()
{
$hewan = Hewan::with('kategori')->orderBy('name', 'ASC')->get();
foreach ($hewan as $item) {
$item->foto = url('images/foto/' . $item->foto);
}
return response()->json([
'success' => true,
'message' => 'List data hewan',
'data' => $hewan
]);
}
public function searchHewan(Request $request)
{
$keyword = $request->keyword;
$hewan = Hewan::with('kategori')
->where('name', 'LIKE', "$keyword%")
->get();
foreach ($hewan as $item) {
$item->foto = url('images/foto/' . $item->foto);
}
return response()->json([
'success' => true,
'message' => 'Hasil pencarian hewan',
'data' => $hewan
]);
}
public function getHewanById($id)
{
$hewan = Hewan::with('kategori')->find($id);
if (!$hewan) {
return response()->json([
'success' => false,
'message' => 'Data hewan tidak ditemukan',
'data' => null
]);
}
$hewan->foto = url('images/foto/' . $hewan->foto);
return response()->json([
'success' => true,
'message' => 'Detail data hewan',
'data' => $hewan
]);
}
}