You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中使用Datum模型插入数据提示data表不存在问题求助

问题原因与解决方案

问题根源

Laravel Eloquent模型默认遵循表名规则:模型类名的复数小写形式作为对应表名。你的模型是Datum,它的复数形式是data,所以框架自动尝试向data表插入数据,但你的实际数据库表是Datum,因此出现表不存在的错误。

解决步骤

  1. 修改Datum模型,指定表名
    在Datum模型类中添加$table属性,明确绑定实际的数据库表名:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Datum extends Model
{
    // 绑定实际存在的表名
    protected $table = 'Datum';

    // 允许批量赋值的字段(必须设置,否则create方法会报错)
    protected $fillable = ['configuration_id', 'student_id', 'puntuacion'];

    // 如果你的表没有默认的created_at/updated_at字段,需添加此行
    // public $timestamps = false;
}
  1. 优化代码(可选)
    你无需对请求数据做json_encode再json_decode的冗余操作,直接提取所需字段即可:
public function postData(Request $request){
    $datos = $request->only(['configuracion_id', 'student_id', 'puntuacion']);
    
    Log::info('Request:'.json_encode($datos));
    
    Datum::create([
        'configuration_id' => $datos['configuracion_id'],
        'student_id' => $datos['student_id'],
        'puntuacion' => $datos['puntuacion'],
    ]);

    return $datos;
}

内容的提问来源于stack exchange,提问作者Luis Yiao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 13:07:06