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

Laravel多数组插入MySQL及Trying to access错误解决求助

Hey there! Let's work through your Laravel + MySQL questions one by one.

问题1:解决表单插入时的 "Trying to access array offset on value of type int" 错误

First up, let's fix that frustrating error you're seeing. The root issue is a mix-up in how you're accessing your array values in the loop.

Why the error happens

In your foreach ($request->get('score') as $key => $value) loop, $key is an integer (like 0, 1, 2...) representing the index of each score in the array. But you wrote $key[$class]—this tries to treat the integer $key as an array and access the "offset" named $class, which doesn't make sense. Integers aren't arrays, hence the error.

You need to flip it around: use $key as the index to access your $class, $year, and $subject_id arrays instead.

Fixed backend code

$class = $request->get("class"); 
$year = $request->get("year"); 
$subject_id = $request->get("subject_id"); 
$scores = $request->get('score');

foreach ($scores as $key => $score) {
    // Skip any incomplete records to avoid index errors
    if (!isset($class[$key], $year[$key], $subject_id[$key])) {
        continue;
    }
    
    SubmitScores::create([ 
        'class' => $class[$key], 
        'year' => $year[$key], 
        'subject_id' => $subject_id[$key],
        'score' => $score // Don't forget to save the score! Your original code missed this
    ]); 
}

A quick note: I added the score field to the create call—you had it in the form but weren't saving it to the database, so I fixed that for you.

问题2:Efficiently insert multiple arrays in Laravel

The fixed loop above will work, but it runs a separate SQL query for every record. If you're dealing with lots of data, this can be slow. A better approach is bulk insertion, which runs just one SQL query for all records.

Bulk insertion implementation

$class = $request->get("class"); 
$year = $request->get("year"); 
$subject_id = $request->get("subject_id"); 
$scores = $request->get('score');

$batchData = [];
foreach ($scores as $key => $score) {
    if (!isset($class[$key], $year[$key], $subject_id[$key])) {
        continue;
    }
    
    $batchData[] = [
        'class' => $class[$key],
        'year' => $year[$key],
        'subject_id' => $subject_id[$key],
        'score' => $score,
        'created_at' => now(), // Required if using bulk insert (auto-timestamps don't fire)
        'updated_at' => now()
    ];
}

// Only run the insert if we have valid data
if (!empty($batchData)) {
    SubmitScores::insert($batchData);
}

Important checks

  • Make sure your SubmitScores model allows mass assignment for all these fields. Add them to the $fillable array:
    class SubmitScores extends Model
    {
        protected $fillable = ['class', 'year', 'subject_id', 'score'];
    }
    
  • When using insert() instead of create(), Laravel won't auto-fill the created_at and updated_at timestamps. That's why we added them manually. If you don't want to do this, you can disable timestamps on the model (but that's not recommended for most apps).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 16:42:27