Please, help! thanks…
I learing Laravel at the moment, so doing this CRUD excersice and the view (user.blade.php) is showing me an error messaje: "ErrorException – Undefined variable $empleado"
I’ve screahed everywhere about this error, I’ve checked so many times the controller and the web.php and I still don’t know where is error is comming from… I gonna leave bellor the code form the view, the controller and the web.php.
-> Controller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsEmpleado;
class EmpleadosController extends Controller
{
/**
* index para mostrar empleado
* Store para guardar empleado
* update para actualizar un empleado
* destroy para eliminar un empleado
* edit para mostrar el formulario de edicion
*/
public function store(Request $request){
$request -> validate([
'nombre' => 'required|min:3',
'apellido' => 'required|min:3',
'cargo' => 'required',
'ciudad' => 'required',
'pais' => 'required',
'genero' => 'required',
]);
$empleado = new Empleado;
$empleado ->nombre = $request->nombre;
$empleado ->apellido = $request->apellido;
$empleado ->cargo = $request->cargo;
$empleado ->ciudad = $request->ciudad;
$empleado ->pais = $request->pais;
$empleado ->genero = $request->genero;
$empleado ->save();
return redirect()->route('create')->with('success','Se ha creado un nuevo empleado correctamente');
}
public function index(){
$empleados = Empleado::all();
return view('form.list', ['empleados' => $empleados]);
}
public function show($id){
$empleado = Empleado::find($id);
return view('form.user', ['empleado' => $empleado]);
}
}
-> view (user.blade.php)
@extends('app')
@section('content')
<div class ="container w-25 border p-3 mt-4">
<form method="POST" action="{{ route('modify', ['id' => $empleado->id]) }}">
@method('PATCH')
@csrf
@if (session('success'))
<h6 class="alert alert-success">{{ session('success') }}</h6>
@endif
<div class="mb-3">
<h3> Modificar perfil </h3>
</div>
<div class="form-floating mb-3">
<input type="text" name="nombre" class="form-control" value="{{ $empleado->nombre }}">
</div>
<div class="form-floating mb-3">
<input type="text" name="apellido" class="form-control" placeholder="apellido">
<label for="apellido">Apellido</label>
</div>
<div class="dropdown mb-3">
<div class="form-floating">
<select type="text" name="cargo" class="form-select" aria-label="Floating label select example">
<option selected>Profesional</option>
<option value="supervisor">Supervisor</option>
<option value="gerente">Gerente</option>
<option value="presidente">Presidente</option>
</select>
<label for="cargo">Seleccionar Cargo</label>
</div>
</div>
<div class="dropdown mb-3">
<div class="form-floating">
<select type="text" name="pais" class="form-select" aria-label="Floating label select example">
<option selected value="colombia">Colombia</option>
<option value="mexico">Mexico</option>
<option value="argentina">Argentina</option>
<option value="chile">Chile</option>
</select>
<label for="pais">Seleccionar País</label>
</div>
</div>
<div class="dropdown mb-3">
<div class="form-floating">
<select type="text" name="ciudad" class="form-select" aria-label="Floating label select example">
<option selected value="bogota">Bogotá</option>
<option value="df">DF Ciudad de Mexico</option>
<option value="buenos aires">Buenos Aires</option>
<option value="santiago chile">Santiago de Chile</option>
</select>
<label for="ciudad">Seleccionar Ciudad</label>
</div>
</div>
<div class="dropdown mb-3">
<div class="form-floating">
<select type="text" name="genero" class="form-select" aria-label="Floating label select example">
<option selected value="na">n/a</option>
<option value="H">Hombre</option>
<option value="M">Mujer</option>
</select>
<label for="genero">Seleccionar Genero</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Modificar</button>
<button type="button" class="btn btn-secondary">Regresar</button>
</form>
</div>
@endsection
-> web.php
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersEmpleadosController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/create', function () {
return view('form.index');
});
Route::get('/login', function () {
return view('login.login');
});
Route::get('/list', function () {
return view('form.list');
});
Route::get('/modify', function () {
return view('form.user');
});
Route::get('/modify/{id}', [EmpleadosController::class, 'show'])->name('modify');
Route::patch('/modify/{id}', [EmpleadosController::class, 'update'])->name('user-update');
Route::get('/list', [EmpleadosController::class, 'index'])->name('list');
Route::post('/create', [EmpleadosController::class, 'store'])->name('create');
Route::delete('/list', [EmpleadosController::class, 'store'])->name('destroy');
I’ve already tried @foreach
…
2
Answers
Did you get the error when accessing
/modify
or/modify/someID
? If you’re getting the error when accessing the/modify
, then it would be expected since you define the route to return the same blade view (form.user
) but without passingempleado
data.-> I think You need to use below query in controller
-> The compact() function will create a variable in the scope of the Blade template for each of the variables that you pass to it.
-> Once you have passed the $empleado variable to the Blade template, you can use it like any other variable:
-> I think this will help you to solve this error,