Laravel显示了一个不正确的消息 [是混淆]

huangapple go评论68阅读模式
英文:

Laravel shows me an incorrect message [Is confuse]

问题

Laravel错误页面显示了一个不正确的错误消息,消息内容如下:

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

但是我没有一个名为"id"的列,正确的列名是id_doctor。我已经正确编写了SQL查询(Query Builder),但Laravel认为它是错误的。

这是我的代码,该函数:

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'));
}

我已经尝试了以下命令,我认为这应该是会话错误:

首先:

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]
 
然后:

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

然后,我关闭并重新打开Xampp的连接,并从Sublime Text中删除项目文件夹,然后重新添加。但什么也没改变。

这是路由:

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

如果我将editarMedico改为edit,那么错误会显示:

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

这个函数是问题的原因。Laravel显示了一个错误的视图错误。如果函数editarMedico为空,错误消息不会改变。

此外,我如何跟踪错误消息,跟踪精确的路由或控制器?

我不知道为什么Laravel显示了一个不正确的错误。我应该怎么做?

英文:

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

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

Laravel显示了一个不正确的消息 [是混淆]

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?

答案1

得分: 1

当您使用模型绑定时,像这样。

public function editarMedico(Medicos $medicos) {

它将自动根据模型类中的属性在数据库中获取模型。由于您没有指定它,Laravel 认为主键是 id

要解决此问题,在 Medicos.php 类中,将您的主键设置为正确的值。

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

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';
}

huangapple
  • 本文由 发表于 2023年5月7日 07:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定