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

Laravel表单提交到控制器仅返回_token无其他输入值如何解决

问题原因及修复方案

你的问题是HTML结构错误、提交按钮配置错误共同导致的表单参数无法正常提交,具体问题和修复方法如下:

存在的问题

  • 提交按钮不在表单范围内,且额外添加了跳转逻辑:你的type="submit"按钮写在了</form>闭合标签外面,无法关联触发表单提交;同时按钮加了onclick="location.href='/cars'",点击后直接跳转页面,表单根本不会执行提交动作。
  • HTML结构不符合规范:<form>直接嵌套在<tr>标签下不符合HTML语法要求,浏览器解析时会自动把<form>移到<table>外部,导致所有表单项脱离表单范围,提交时自然只会携带写在form内的@csrf生成的_token参数。
  • 表单提交路径参数未替换:action属性写的/cars/{id}/edit中的{id}是占位符,没有替换为实际的车辆ID,提交的请求路径本身就是错误的。
  • 模板变量被覆盖:循环写法@foreach ($cars as $cars)会把原始的车辆集合变量覆盖,会引发后续渲染异常。
  • 控制器更新逻辑不完整:直接调用$car->update()没有传入要更新的参数,就算拿到表单数据也无法完成更新。

修复代码示例

修正后的模板代码

@extends('dashboard.base')

@section('content')
  <div class="container-fluid">
    <div class="fade-in">
      <div class="card">
        <div class="card-header"> edit car</div>
          <div class="card-body">
            <div class="body">
              <table class="table">
                <thead>
                  <tr>
                    <th scope="col">#</th>
                    <th scope="col">title</th>
                    <th scope="col">content</th>
                    <th scope="col">youtube</th>
                    <th scope="col">banner image</th>
                    <th scope="col">extra info</th>
                    <th scope="col">location</th>
                    <th scope="col">pricing</th>
                  </tr>
                </thead>
                <tbody>
                  @if (count($cars) != 0)
                    {{-- 修正循环变量名,避免覆盖原集合 --}}
                    @foreach ($cars as $car)
                      <tr>
                        <th scope="row">#</th>
                        <th >{{ $car->title }}</th>
                        <td>{{ $car->content }}</td>
                        <td>{{ $car->youtubevid }}</td>
                        <td>{{ $car->bannerimage }}</td>
                        <td>{{ $car->extrainfo }}</td>
                        <td>{{ $car->location }}</td>
                        <td>{{ $car->car_price }}</td>
                      </tr>
                    @endforeach
                  @endif
                </tbody>
              </table>
              {{-- 把form移到表格外部,修正action的ID参数 --}}
              <form method="post" action="{{ url('cars/'.$id.'/edit') }}" enctype="multipart/form-data">
                @csrf
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col">#</th>
                      <th scope="col"><input type="text" id="title" name="title" size="6" placeholder="new title"></th>
                      <th scope="col"><input type="text" name="content" size="6" placeholder="new content"></th>
                      <th scope="col"><input type="text" name="youtube" size="6" placeholder="new youtube"></th>
                      <th scope="col"><input type="text" name="bannerimage" size="6" placeholder="new banner image"></th>
                      <th scope="col"><input type="text" name="extrainfo" size="6" placeholder="new extra info"></th>
                      <th scope="col"><input type="text" name="location" size="6" placeholder="new location"></th>
                      <th scope="col"><input type="text" name="pricing" size="6" placeholder="new pricing"></th>
                    </tr>
                  </thead>
                </table>
                {{-- 提交按钮放在form内部,去掉多余跳转逻辑 --}}
                <button class="btn btn-danger" type="submit">update</button>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection
@section('javascript')
  <script src="{{ asset('js/colors.js') }}"></script>
@endsection

修正后的控制器代码

public function carEdit(Request $request, $id)
{
    $data = $request->input();
    dd($data); // 此时可以正常打印所有表单参数
    $car = Cars::findOrFail($id);
    // 匹配表单字段名和表字段名完成赋值
    $car->update([
        'title' => $data['title'],
        'content' => $data['content'],
        'youtubevid' => $data['youtube'],
        'bannerimage' => $data['bannerimage'],
        'extrainfo' => $data['extrainfo'],
        'location' => $data['location'],
        'car_price' => $data['pricing'],
    ]);
    // 查询全量车辆数据返回视图
    $cars = Cars::all();
    return view('/dashboard/editCars')->with('cars',$cars);
}

内容的提问来源于stack exchange,提问作者Mhamad Abu AlGhanam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 16:06:00