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

Laravel 9 添加商品评价功能点击提交出现404错误求助

Laravel 9 用户评价功能提交404错误排查与解决

问题描述

在Laravel 9应用中开发用户评价/评分功能时,点击提交评分按钮触发404|Not Found错误。核心信息:

  • 控制器处理方法名为store,与blade视图表单提交目标一致
  • 单用户可评价多商品,评价权限仅限登录用户

相关代码展示

Blade视图代码

<h2>{{ $product->name }}</h2>

<p>{{ $product->description }}</p>

<h3>评价列表</h3>

<ul>
    @foreach ($product->reviews as $review)
        <li>{{ $review->review }} - {{ $review->rating }} 星</li>
    @endforeach
</ul>

<h3>添加评价</h3>

<form method="POST" action="/products/{{ $product->id }}/reviews">
    @csrf

    <div>
        <label for="review">评价内容</label>
        <textarea name="review" id="review">{{ old('review') }}</textarea>

        @error('review')
            <div>{{ $message }}</div>
        @enderror
    </div>

    <div>
        <label for="rating">评分</label>
        <input type="number" name="rating" id="rating" min="1" max="5" value="{{ old('rating') }}">

        @error('rating')
            <div>{{ $message }}</div>
        @enderror
    </div>

    <button type="submit">提交评价</button>
</form>

控制器代码

public function store(Product $product)
{
    $data = request()->validate([
        'review' => 'required|string',
        'rating' => 'required|integer|min:1|max:5',
    ]);

    $review = new Review($data);
    $review->user_id = auth()->user()->id;
    $product->reviews()->save($review);

    return back();
}

路由代码

Route::post('/products/{product}/reviews', [HomeController::class, 'store']);
Route::middleware(['auth'])->group(function () {
    Route::post('/products/{product}/reviews', [HomeController::class, 'store']);
});

数据库迁移结构

$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('user_id');
$table->text('review');
$table->integer('rating');
$table->timestamps();

$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

排查与解决步骤

1. 修复重复路由问题

当前定义了两条完全相同的POST路由,第一条未加登录校验,第二条在auth中间件组内,会导致路由匹配逻辑混乱。直接删除第一条无中间件的路由,保留auth组内的路由即可(符合“需登录才能评价”的需求):

Route::middleware(['auth'])->group(function () {
    Route::post('/products/{product}/reviews', [HomeController::class, 'store']);
});

2. 验证路由有效性

运行命令php artisan route:list,确认POST /products/{product}/reviews路由存在,且控制器与方法映射为HomeController@store。

3. 优化表单提交路径

避免硬编码URL,改用路由命名+辅助函数生成路径,减少路径错误概率:

  • 给路由添加名称:
Route::middleware(['auth'])->group(function () {
    Route::post('/products/{product}/reviews', [HomeController::class, 'store'])->name('products.reviews.store');
});
  • 修改blade表单的action属性:
<form method="POST" action="{{ route('products.reviews.store', $product) }}">

4. 检查控制器命名空间

确保路由文件顶部已正确引入HomeController:

use App\Http\Controllers\HomeController;

5. 确认模型关联正确性

  • 在Product模型中定义与Review的关联:
public function reviews()
{
    return $this->hasMany(Review::class);
}
  • 在Review模型中定义反向关联:
public function product()
{
    return $this->belongsTo(Product::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 06:40:26