'Problem passing data from the controller to the view Laravel 8
I'm getting an error when I try to pass data from the controller to the view and I spent a lot of time trying to solve it. The error is "Undefined Variable $instEntNacs". There is other form this view, because depending on the rol, it will show the first or the second one.
Here's the controller "ConvenioController":
public function create()
{
$instEntNacs = DB::table('inst_ent_nacs')->select('id', 'nombre')->get();
return view('convenios.create', ['instEntNacs' => $instEntNacs]);
}
And the view:
@if (auth()->user()->rol_id == "2")
<form method="POST" class="form-conv-nac border border-2 rounded-3 shadow-lg" action="{{ route('convenios.store_nac') }}" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="offset-1 col-10 mt-4">
<select class="form-control border border-dark" id="con_instEntNac" name="con_instEntNac">
<option selected value="">-- Institución o Entidad --</option>
@foreach ($instEntNacs as $item)
<option value="{{ $item->id }}"> {{ $item->nombre }}</option>
@endforeach
</select>
</div>
</div>
<div class="row mt-4 mb-5">
<div class="offset-1 col-2">
<a href="{{ route('login.activites') }}">Regresar</a>
</div>
<div class="offset-2 col-3">
<button type="submit" class="w-100 btn_2 btn-danger rounded-pill border border-dark">Cancelar</button>
</div>
<div class="col-3">
<button type="submit" class="w-100 btn_1 btn-primary rounded-pill border border-dark">Registrar</button>
</div>
</div>
</form>
@endif
Solution 1:[1]
Have you tried this?
$data['instEntNacs'] = DB::table('inst_ent_nacs')->select('id', 'nombre')->get();
return view('convenios.create')->with($data);
Solution 2:[2]
You can try compact('instEntNacs') with return view('convenios.create').
return view('convenios.create', compact('istEntNacs'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MamaWin |
| Solution 2 | geertjanknapen |
