I'm trying to show data from a database in a view where the view isn't showing a thing, except the HTML code for it. This is an piece of the code I'm trying to make it work.
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Nombre del Indicador:</strong>
{{ $indicadorFinanciero->nombreIndicador }}
</div>
</div>
I get no error from Laravel saying that something is wrong, but clearly there is something messed up with the variables, or the way I'm calling them, because when I tweak them, and try to change it, I get errors from Laravel.
What is weird for me is that I already manage to call the data from the Database and show it in a index view, I made the difference, and I went for it using a foreach loop.
What I'm trying to do is to do the same as in my index view but only show it (or read it, if we take into account that it is a CRUD I'm making). the code for the index is:
@extends('indicadoresfinancieros.layout')
@section('content')
<div class="row">
<div class="col-lg-12">
<div class="float-start">
<h2>Indicadores Financieros</h2>
</div>
<div class="float-end" style="margin-bottom: 15px">
<a href="{{ route('indicadoresfinancieros.create') }}" class="btn btn-success">Crear Nueva entrada</a>
</div>
</div>
</div>
@if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Nombre del Indicador</th>
<th>Codigo del Indicador</th>
<th>Unidad de medida del Indicador</th>
<th>Valor del Indicador</th>
<th>Fecha de Registro</th>
<th width="280px">Accion</th>
</tr>
@foreach ($indicadoresfinancieros as $indicadorfinanciero)
<tr>
<td>{{ $indicadorfinanciero->id }}</td>
<td>{{ $indicadorfinanciero->nombreIndicador }}</td>
<td>{{ $indicadorfinanciero->codigoIndicador }}</td>
<td>{{ $indicadorfinanciero->unidadMedidaIndicador }}</td>
<td>{{ $indicadorfinanciero->valorIndicador }}</td>
<td>{{ $indicadorfinanciero->fechaRegistro }}</td>
<td>
<form action="{{ route('indicadoresfinancieros.destroy', $indicadorfinanciero->id) }}" method="POST">
<a href="{{ route('indicadoresfinancieros.show', $indicadorfinanciero->id) }}" class="btn btn-info">Mostrar</a>
<a href="{{ route('indicadoresfinancieros.edit', $indicadorfinanciero->id) }}" class="btn btn-primary">Editar</a>
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Borrar</button>
</form>
</td>
</tr>
@endforeach
</table>
<div class="mx-auto pb-10 w-4/5">
{{ $indicadoresfinancieros->links('pagination::bootstrap-5'); }}
</div>
@endsection
As we see I'm a going through with the foreach, but in this case the variables are different, because I took from my IndicadorFinancieroController where I saved some data in the $indicadoresfinancieros variable.
<?php
namespace App\Http\Controllers;
use App\Models\IndicadorFinanciero;
use Illuminate\Http\Request;
class IndicadorFinancieroController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//tomar datos y mostrarlos
$indicadoresfinancieros = IndicadorFinanciero::orderBy('id')->Paginate(5);
return view('indicadoresfinancieros.index', compact('indicadoresfinancieros'))->with(request()->input('page'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('indicadoresfinancieros.create');
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//validar
$request->validate([
'nombreIndicador'=>'required',
'codigoIndicador'=>'required',
'unidadMedidaIndicador'=>'required',
'valorIndicador'=>'required',
'fechaIndicador'=>'required'
]);
//crear entrada
IndicadorFinanciero::create($request->all());
//redirigir
return redirect()->route('indicadoresfinancieros.index')->with('success', 'Entrada creada exitosamente');
}
/**
* Display the specified resource.
*/
public function show(IndicadorFinanciero $indicadorFinanciero)
{
return view('indicadoresfinancieros.show', compact('indicadorFinanciero'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(IndicadorFinanciero $indicadorFinanciero)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, IndicadorFinanciero $indicadorFinanciero)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(IndicadorFinanciero $indicadorFinanciero)
{
//
}
}
use @foreach ($indicadoresfinancieros->data as $indicadorfinanciero)
instead of @foreach ($indicadoresfinancieros as $indicadorfinanciero)