0

As the title says with other controller and view I can send the data but I can't with the controller and view I'm gonna post below.

Controller code (Its a resource controller but I'm using a custom function)

<?php

namespace App\Http\Controllers;
use App\Models\Persona;
use Illuminate\Http\Request;

class PersonaController extends Controller
{

public function mostrarMedicos(){
    $medicos = Persona::where('idTipoPersona', 4)->get();
    return view('gestionMedicos',compact($medicos));

} }

View Code

@extends('layouts.app')

@section('content')

<h1>Gestión Médicos</h1>
<div class="container">
    <table class="table table-bordered table-hover">
        <tr class="info">
            <th>Nombre</th>
            <th>Apellido</th>
            <th>Cedula</th>
            <th>Email</th>
            <th>Teléfono</th>
            <th>Dirección</th>
            <th>Ciudad Residencia</th>
            <th>Fecha de Nacimiento</th>
            <th>Género</th>
        </tr>
        @foreach ($medicos as $medico)
            <tr>
                <td>{{$medico->nombre}}</td>
                <td>{{$medico->apellido}}</td>
                <td>{{$medico->cedula}}</td>
                <td>{{$medico->email}}</td>
                <td>{{$medico->telefono}}</td>
                <td>{{$medico->direccion}}</td>
                <td>{{$medico->ciudadResi}}</td>
                <td>{{$medico->fechaNacimiento}}</td>
                <td>{{$medico->genero}}</td>
            </tr>
        @endforeach
    </table>

Route

Route::get('/gestionarMedicos', [PersonaController::class,'mostrarMedicos'])->name('personaMostrarMedicos');

This is the error I'm getting

Undefined variable $medicos (View: D:\xampp\htdocs\SistemaHNF\resources\views\gestionMedicos.blade.php)

Im new on Laravel and I don't understand why I get this error if from what I can understand the controller is supossed to return the view with the $medicos variable that should have the data.

(English is not my main language so sorry for any mistake and also I can post any extra code or explain in more detail something if needed).

1
  • return view('gestionMedicos',compact('medicos')); simple just remove $ sign Commented Jun 26, 2021 at 3:43

1 Answer 1

4

You have return query builder and forgotten to call get() or first() and also wrong with compact

public function mostrarMedicos(){
        $medicos = Persona::where('idTipoPersona', 4)->get();
        return view('gestionMedicos',compact('medicos'));
}
Sign up to request clarification or add additional context in comments.

3 Comments

already did it but still getting error. Going to update the question to reflect the change
@DiegoUtreras.you have issue with compact. check my answer again
yeah that was the iisue @John, thanks for helping. It needs the variable as a String in the compact method, my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.