简体   繁体   中英

Laravel Delete Route 404 Not Found

Why am I trying to remove the product from the wishlist, I get a 404 error?

In my home.php, I have this:

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

In my controller I have this:

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

form:

               <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>

Your wishlist should something like /profile/wishlist/{wishlist} So in this case delete redirect back to this wishlist but you already delete that so it will show 404. You have show probably use some index there or retirect to another route.

route('profile.wishlist.index');

Then it will show index which you can configure to show all wishlist or you can do it you own way.

Because at the end you go back to the previous page and the previous page was deleted to the element and. No longer available

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM