按钮未重定向 [英] Button not redirecting

查看:66
本文介绍了按钮未重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订购按钮,该按钮应该将用户重定向到已订购商品的购物车页面

i have an order button that is supposed to redirect the user to the cart page with the items ordered

<p class="btn-holder"><a href="{{route('addCart',$food->id) }}" class="btn btn-primary btn-block text-center" role="button" > Order this</a> </p>

这是web.php上的路由

This is the route on web.php

Route::get('add-to-cart/{id}', 'FoodsController@addToCart')->name('addCart');

这是addToCart函数

This is the function addToCart

public function addToCart($id){
        $food = Food::find($id);   

        if(!$food) {

            abort(404);

        }

        $cart = session()->get('cart');

        // if cart is empty then this the first product
        if(!$cart) {

            $cart = [
                    $id => [
                        // "productId" => $food->id,
                        "name" => $food->food_item,
                        "quantity" => 1,
                        "price" => $food->price,

                    ]
            ];


            session()->put('cart', $cart);


            return redirect()->back()->with('success', 'Product added to cart successfully!');
        }

        // if cart not empty then check if this product exist then increment quantity
        if(isset($cart[$id])) {

            $cart[$id]['quantity']++;

            session()->put('cart', $cart);

            return redirect()->back()->with('success', 'Product added to cart successfully!');

        }

        // if item not exist in cart then add to cart with quantity = 1
        $cart[$id] = [
            // "productId" => $food->id,
            "name" => $food->food_item,
            "quantity" => 1,
            "price" => $food->price,

        ];


        session()->put('cart', $cart);

        return redirect()->back()->with('success', 'Product added to cart successfully!');
    }

但是当我单击按钮时,它不会重定向到购物车页面,它会继续加载到同一位置 我做到了

But when i click the button it doesn't redirect to the cart page it keeps loading to the same spot i did

 dd($food); 

在功能上,它会输出正确的结果

on the fuction and it outputs right results

推荐答案

我有一个订购按钮,该按钮应该将用户重定向到已订购商品的购物车页面

I have an order button that is supposed to redirect the user to the cart page with the items ordered

使用return redirect()->back()->with...时的操作是将用户 back 重定向到他们来自的页面,在本例中为Order this按钮所在的页面.

What you are doing when you use return redirect()->back()->with... is you are redirecting the user back to the page they came from, in this case the page where your Order this button is.

所以目前的逻辑是:

  1. 在产品页面上
  2. 用户单击Order this按钮
  3. 该产品已添加到购物车中,或已递增
  4. 然后将用户重定向回上一页
  1. On product page
  2. User clicks Order this button
  3. The product is either added to the cart, or incremented
  4. The user is then redirected back to the previous page

如果您希望在单击Order this按钮时将用户重定向到另一个页面,则需要将用户重定向到另一个页面或另一个路由,例如

If you want the user to be redirected to another page when clicking the Order this button, you will need to redirect the user to either another page, or another route, like so

return redirect()->route('View Cart')->with('success', 'Product added to cart successfully!');

或类似这样:

return redirect('/view-cart')->with('success', 'Product added to cart successfully!');

应导致以下流程:

  1. 在产品页面上
  2. 用户单击Order this按钮
  3. 该产品已添加到购物车中,或已递增
  4. 然后将用户重定向到查看购物车页面
  1. On product page
  2. User clicks Order this button
  3. The product is either added to the cart, or incremented
  4. The user is then redirected to the view cart page

我不知道你有什么路线,所以以上是一个近似值.

I don't know what routes you have, so the above is an approximation.

这篇关于按钮未重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆