ErrorException - Undefined variable $empleado(Laravel 8 + PHP 8 + SQLite)

gkl3eglg  于 2023-11-20  发布在  PHP
关注(0)|答案(2)|浏览(204)

救命啊!谢谢……
I learing Laravel at the moment,so doing this CRUD exercise 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 coming from. I gonna leave bellor the code form the view,the controller and the web.php.
->控制器

<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\Models\Empleado; 

    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 Illuminate\Support\Facades\Route;
     use App\Http\Controllers\EmpleadosController;
/*
|--------------------------------------------------------------------------
| 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');


我已经试过@foreach

eh57zj3b

eh57zj3b1#

访问/modify/modify/someID时是否出现错误?如果访问/modify时出现错误,那么这是正常的,因为您定义了返回相同刀片视图(form.user)但不传递empleado数据的路由。

Route::get('/modify', function () {
    return view('form.user');
});

字符串

6gpjuf90

6gpjuf902#

->我认为您需要在控制器中使用以下查询

public function show($id)
{
    $empleado = Empleado::find($id);

    return view('form.user', compact('empleado'));
}

字符串
-> compact()函数将在Blade模板的作用域中为传递给它的每个变量创建一个变量。
->将$empleado变量传递给Blade模板后,您可以像使用其他变量一样使用它:

{{ $empleado->nombre }}


“我认为这将有助于你解决这个错误。

相关问题