This is a simple API that returns a list of dog breeds. Please note that not all images load with the correct dog breed due to API changes.
Breeds and images come from The Dog API (with a Dog CEO fallback for breed photos). Here is the real PHP behind it.
public function index(): View { try { $breeds = $this->dogApiService->getBreeds(); $randomDogImage = $this->dogApiService->getRandomDogImage(); return view('dogs', [ 'heading' => 'Dogs', 'breeds' => $breeds, 'dogImage' => $randomDogImage ]); } catch (RequestException $e) { // Log the error logger()->error('Dog API Error', [ 'message' => $e->getMessage(), 'code' => $e->getCode() ]); return view('dogs', [ 'heading' => 'Dogs', 'breeds' => [], 'dogImage' => null, 'error' => 'Unable to fetch dog information. Please try again later.' ]); } }
public function __construct() { $apiUrl = self::API_BASE_URL; Http::macro('dogApi', function () use ($apiUrl) { return Http::withHeaders([ 'x-api-key' => config('services.dog_api.key') ])->baseUrl($apiUrl); }); }
public function getBreeds(): Collection { $response = Http::dogApi() ->get('/breeds') ->throw(); return collect($response->json()); }
public function getRandomDogImage(): ?array { $response = Http::dogApi() ->get('/images/search') ->throw(); return $response->json()[0] ?? null; }
public function getBreedImage(string $breedId): ?array { $response = Http::dogApi() ->get('/images/search', ['breed_ids' => $breedId]) ->throw(); return $response->json()[0] ?? null; }