EVOLUTION-MANAGER
Edit File: noticias.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Informativo; use App\Info_imagen; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class noticias extends Controller { public function index() { $datosnoticias = Informativo::select( 'informativo.id_informativo', 'informativo.titulo', 'informativo.fecha', 'info_imagenes.nombre_archivo', DB::raw('(Case when month(informativo.fecha) = 1 then "Enero" end) as mes') ) ->join('info_imagenes', 'informativo.id_informativo', '=', 'info_imagenes.id_informativo') ->where('info_imagenes.orden', 1) ->orderBy('informativo.fecha', 'desc') ->orderBy('id_informativo', 'desc') ->paginate(6); return view('web-patronato-informativo/noticias', compact('datosnoticias')); } // public function show($id) // { // $valor = $id; // $previus = true; // $next = true; // $anterior = 0; // $siguiente = 0; // $indice = Informativo::select('id_informativo', 'fecha') // ->orderBy('fecha', 'desc') // ->get(); // $id_noti = 0; // $cont = 0; // foreach ($indice as $ide) { // if ($ide->id_informativo == $valor) { // $valor = $ide->id_informativo; // $cont = $id_noti; // } // $id_noti++; // } // if (count($indice) > 0) { // if ($cont - 1 >= 0) { // $next = false; // $siguiente = $indice[$cont - 1]->id_informativo; // } // if ($cont + 1 < count($indice)) { // $previus = false; // $anterior = $indice[$cont + 1]->id_informativo; // } // } // $datosnoticias = Informativo::select('id_informativo', 'titulo', 'contenido', 'fecha') // ->where('id_informativo', $valor) // ->get(); // $imagen = Info_imagen::select('nombre_archivo', 'orden', 'id_informativo') // ->where('id_informativo', $valor) // ->orderBy('orden', 'asc') // ->get(); // return view('web-patronato-informativo/detallesnoticas', compact('datosnoticias', 'imagen', 'valor', 'anterior', 'previus', 'siguiente', 'next')); // } public function show($id) { try { // Obtener todas las noticias ordenadas por fecha descendente $noticias = Informativo::orderBy('fecha', 'desc')->orderBy('id_informativo', 'desc')->pluck('id_informativo')->toArray(); // Encontrar el índice de la noticia actual $currentIndex = array_search($id, $noticias); // Variables para la noticia anterior y siguiente $anterior = $siguiente = null; $previus = $next = true; // Determinar la noticia siguiente (más reciente) y anterior (menos reciente) if ($currentIndex !== false) { if (isset($noticias[$currentIndex - 1])) { $siguiente = $noticias[$currentIndex - 1]; $next = false; } if (isset($noticias[$currentIndex + 1])) { $anterior = $noticias[$currentIndex + 1]; $previus = false; } }else return redirect()->route('all-noticias'); // Obtener la noticia actual $datosnoticia = Informativo::select('id_informativo', 'titulo', 'contenido', 'fecha') ->where('id_informativo', $id) ->get(); // Obtener las imágenes de la noticia $imagenes = Info_imagen::where('id_informativo', $id) ->orderBy('orden', 'asc') ->get(['nombre_archivo', 'orden']); // Retornar la vista con los datos necesarios return view('web-patronato-informativo/detallesnoticas', [ 'datosnoticias' => $datosnoticia, 'imagen' => $imagenes, 'valor' => $id, 'anterior' => $anterior, 'previus' => $previus, 'siguiente' => $siguiente, 'next' => $next, ]); } catch (\Exception $e) { // Manejar errores en caso de que algo falle Log::error('Algo salió mal: ' . $e->getMessage()); return redirect()->route('all-noticias'); } } }