Search code examples
phplaravellaravel-bladelaravel-query-builder

Laravel shows me an incorrect message [Is confuse]


Laravel error page shows me an incorrect error message, the messages says:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'

enter image description here

But I don't have a column id with the name "id" the correct name is id_doctor. I have correctly written the SQL Query (Query Builder), but Laravel thinks it's wrong.

This is my code, the function.

 public function editarMedico(Medicos $medicos){

    $datos = DB::table('medicos')
    ->join('tipo_medico','medicos.especialidad_id','=','tipo_medico.id_tipo_medico')
    ->selectRaw("medicos.id_doctor, CONCAT(medicos.nombre,' ',medicos.apellidos) AS nombre_medico, rut, fecha_nacimiento, tipo_medico.nombre_tipo AS especialidad, estado")
    ->where('medicos.id_doctor', '=' ,$medicos->id_doctor)->first();

    return view('forms.editar-horarios', compact('datos','medicos'));
    
}

I have already tried the following commands, I think it should be an error from the session.

First:

PS C:\xampp\htdocs\MyProject> [ctrl+c, to close batch connection] Do you want to close the connection (S/N)?
PS C:\xampp\htdocs\MyProject> [ctrl+c, to close serve connection]

Then:

PS C:\xampp\htdocs\MyProject> php artisan route:clear
PS C:\xampp\htdocs\MyProject> php artisan view:clear
PS C:\xampp\htdocs\MyProject> php artisan cache:clear
PS C:\xampp\htdocs\MyProject> php artisan config:clear

PS C:\xampp\htdocs\MyProject>php artisan serve
PS C:\xampp\htdocs\MyProject>npm run dev

Then I close and open the connection from Xampp and I remove the project folder from Sublime Text and re-add. But nothing changed.

This is the route.

Route::get('/secretaria/editar/medicos/{medicos}', [MedicosController::class, 'editarMedico'])->name('horarios.editar');

If I change editarMedico to edit then the error says.

Method App\Http\Controllers\Medicos\MedicosController::edit does not exist.

That function is the cause of the problem. Laravel shows me a wrong view error. And If the function editarMedico was empty the error message doesn't change.

Additional, How can I trace the error message, trace the precise route or controller?

I don't know why Laravel show me an incorrect error. What Can I do?


Solution

  • When you use model binding like this.

    public function editarMedico(Medicos $medicos) {
    

    It will automatically fetch the model based on its properties in the model class, in the database. Since you have not specified it Laravel believes that the primary key is id.

    To fix it, in the class Medicos.php. Set you primary key to be the correct one.

    class Medico extends Model {
        protected $primaryKey = 'id_doctor';
    }