@extends('layouts.app') @section('content')

Notícias Voltar

@php $client = new \GuzzleHttp\Client(); try { $response = $client->get("https://cnm.org.br/api/noticias", [ 'headers' => [ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36', 'Accept' => 'application/json', 'Accept-Language' => 'en-US,en;q=0.5', 'Referer' => 'https://cnm.org.br/' ], 'query' => [ 'categoria' => 'mulheres-e-juventude', 'limit' => 12, 'page' => request()->get('page', 1), 'offset' => (request()->get('page', 1) - 1) * 12, 'sort' => 'created_at', 'order' => 'desc' ] ]); $noticias = json_decode($response->getBody(), true); $data = $noticias['noticias']['data'] ?? []; $total = $noticias['noticias']['total'] ?? 0; // Cria uma instância do LengthAwarePaginator $paginator = new \Illuminate\Pagination\LengthAwarePaginator( $data, $total, 12, request()->get('page', 1), ['path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath()] ); } catch (\Exception $e) { $data = collect(); // Coleção vazia $total = 0; $paginator = new \Illuminate\Pagination\LengthAwarePaginator( $data, $total, 12, request()->get('page', 1), ['path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath()] ); $error = "Erro ao buscar notícias: " . $e->getMessage(); } @endphp @if(isset($error))

{{ $error }}

@elseif($paginator->isEmpty())

Nenhuma notícia encontrada

@else @foreach ($paginator as $key => $noticia) @if($key > 0)
@endif @endforeach @endif
{{ $paginator->links('pagination.my-pagination') }}
@endsection