【问题标题】:Laravel Delete Route 404 Not FoundLaravel 删除 Route 404 Not Found
【发布时间】:2022-01-21 13:56:43
【问题描述】:

为什么我要尝试从愿望清单中删除产品,但出现 404 错误?

在我的 home.php 中,我有这个:

Route::prefix('profile')->middleware(['auth'])->group(function () {
    Route::get('', [ProfileController::class, 'index']);
    Route::get('/twofactor', [ProfileController::class, 'manageTwoFactor']);
    Route::post('/twofactor', [ProfileController::class, 'activeTwoFactor']);
    Route::get('/twofactor/phone',[ProfileController::class,'getPhoneNumberVerify'])->name('profile.2fa.phone');
    Route::post('/twofactor/phone',[ProfileController::class,'postPhoneNumberVerify']);
    Route::get('/orders',[OrderController::class,'index']);
    Route::get('/orders/{order}',[OrderController::class,'show'])->name('order.details');
    Route::get('/orders/{order}/payment',[OrderController::class,'payment'])->name('profile.order.payment');
    Route::resource('/wishlist',WishlistController::class)>except(['show','create','update','edit']);}); //wishlist routes

在我的控制器中,我有这个:

 public function destroy(Wishlist $wishlist)
{
    $wishlist->delete();
    return back();
}

表格:

               <div class="flex-grow-one">
                    <form action="{{route('wishlist.destroy',$wish->product_id)}}" 
                       method="post" id="delete-wish-{{$wish->product_id}}">
                        @csrf
                        @method('delete')
                    </form>
                    <a href="javascript:" onclick="document.getElementById('delete-wish- 
                      {{$wish->product_id}}').submit();" class="remove-favorite">
                        <i class="fa fa-trash-alt"></i></a>
              </div>

【问题讨论】:

  • 尝试运行php artisan route:list 并查看销毁路线
  • 可能是 $wishlistWishlist 模型未找到,所以它抛出 404 错误
  • 我已经这样做了,并且有销毁路线

标签: php laravel routes


【解决方案1】:

您的愿望清单应该类似于 /profile/wishlist/{wishlist} 所以在这种情况下,删除重定向回这个愿望清单,但你已经删除了它,所以它会显示 404。你已经显示可能在那里使用了一些索引或退回到另一条路线。

路线('profile.wishlist.index');

然后它将显示索引,您可以将其配置为显示所有愿望清单,也可以按照自己的方式进行。

【讨论】:

  • 我的问题解决了,我不得不把 $wish->id 而不是 $wish->product_id
【解决方案2】:

因为最后你回到上一页并且上一页被删除了元素和。不再可用

public function destroy(Wishlist $wishlist)
{
    $wishlist->delete();
    return back();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 2016-08-21
    • 1970-01-01
    • 2023-03-05
    • 2019-10-05
    • 1970-01-01
    • 2020-04-12
    • 2016-10-26
    相关资源
    最近更新 更多