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

Composer更新后日程数据更新失败且数据丢失问题求助

问题排查:Composer Update后日程更新失败及数据丢失问题

执行composer update后,原正常运行的日程更新功能出现更新失败、数据丢失情况,结合提供的代码,以下是核心问题点及修复方案:

1. HTML嵌套表单导致的误操作(数据丢失直接原因)

edit.blade.php中更新表单内部嵌套了删除表单,违反HTML规范,浏览器解析时会出现异常,可能导致点击「更新」按钮时误触发删除请求,直接造成数据丢失。

修复:

将删除表单移至更新表单外部,避免嵌套:

<!-- 更新按钮 -->
<button type="submit" class="btn btn-primary">更新</button>
</form> <!-- 先闭合更新表单 -->

<!-- 削除ボタン -->
<form action="{{ route('schedules.destroy', $schedule->id) }}" style="background-color: #f0f8ff" method="POST" class="d-inline">
    @csrf
    @method('DELETE')
    <button type="submit" class="btn btn-danger" onclick="return confirm('本当に削除しますか?')">削除</button>
</form>

2. 缺失核心路由(更新失败直接原因)

web.php中仅定义了列表页的GET路由,缺少更新(PUT)和删除(DELETE)的路由配置,导致提交更新/删除请求时路由匹配失败,功能无法正常执行。

修复:

补充缺失的路由,并统一路由命名规范:

// 列表页路由(建议命名为schedules.index,与资源路由规范一致)
Route::get('/schedules', [ScheduleController::class, 'index'])->name('schedules.index');
// 更新路由
Route::put('/schedules/{schedule}', [ScheduleController::class, 'update'])->name('schedules.update');
// 删除路由
Route::delete('/schedules/{schedule}', [ScheduleController::class, 'destroy'])->name('schedules.destroy');

同时将视图中跳转列表页的route('schedule')改为route('schedules.index'),保持命名一致。

3. Blade模板指令顺序错误(错误提示无法显示)

edit.blade.php中@if ($errors->any())错误地放在了@extends('layouts.app')之前,Laravel Blade要求@extends必须是模板的第一个指令,否则会导致错误提示无法渲染,无法看到验证失败的原因。

修复:

调整指令顺序,将错误提示放在@section('content')内部:

@extends('layouts.app')

@section('content')
@if ($errors->any())
    <div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    </div>
@endif
<!-- 其余页面内容 -->
@endsection

4. 模型批量赋值权限缺失(更新不生效)

ScheduleController中使用$schedule->update($validated)进行批量更新,但如果Schedule模型未设置$fillable属性白名单,Laravel的批量赋值保护会阻止字段更新,尤其是Composer Update升级Laravel版本后,严格模式可能默认开启,导致之前的隐式赋值失效。

修复:

在Schedule模型中添加允许批量赋值的字段:

class Schedule extends Model
{
    protected $fillable = ['title', 'description', 'start_time', 'end_time'];
}

5. 日期验证规则不匹配(更新请求被拦截)

页面中使用datetime-local输入框提交的日期格式为Y-m-d\TH:i,但验证规则中仅使用date规则,Composer Update可能升级了Carbon或Laravel验证组件,导致格式校验严格度提升,验证不通过但无法看到错误提示(因模板指令错误)。

修复:

将日期验证规则改为匹配输入格式:

$validated = $request->validate([
    'title' => 'required|string|max:255',
    'description' => 'nullable|string',
    'start_time' => 'required|date_format:Y-m-d\TH:i',
    'end_time' => 'required|date_format:Y-m-d\TH:i|after:start_time',
]);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 22:49:55