【问题标题】:Check Sessions for Shopping Cart检查购物车的会话
【发布时间】:2017-01-06 11:11:10
【问题描述】:

我有一个关于显示添加到购物车的特定问题。首先,我认为以前的路线总是相同的,因为你先选择菜单类别,然后再去菜,所以我无法检查。 我有两种不同的方式去购买产品。第一个是单击导航栏中的“菜单”,然后将菜肴作为画廊打开(没有添加到购物车的选项),第二个是在完成预订表格后选择产品并选择您想要的菜单进行预订.那么,如果我直接从导航栏进入,如何禁用添加到购物车选项????(因为在这两种情况下都是相同的视图) 这是我的控制器:

public function index($id)
{
    $menu_categories = Menu_Categories::where('visible','yes')->where('delete','no')->orderBy('position','ASC')->get();
    $selected_menu = $id;
    $dishes = Dishes::where('visible','yes')->where('delete','no')->where('id_menu_category',$id)->orderBy('name','DESC')->get();

    return view('dishes.dishes', ['dishes' => $dishes,'menu_categories' => $menu_categories,'selected_menu' => $selected_menu]);
}


/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index_nav($id)
{
    $menu_categories = Menu_Categories::where('visible','yes')->where('delete','no')->orderBy('position','ASC')->get();
    $selected_menu = $id;
    $dishes = Dishes::where('visible','yes')->where('delete','no')->where('id_menu_category',$id)->orderBy('name','DESC')->get();
    session(['menu_nav' => true]);
    return view('dishes.dishes', ['dishes' => $dishes,'menu_categories' => $menu_categories,'selected_menu' => $selected_menu]);
}

这是我的观点:

 @foreach($dishes as $dish)
                    <!-- shop item -->
                        <div class="col-md-6 col-sm-6">
                            <div class="home-product text-center position-relative overflow-hidden margin-ten no-margin-top">
                                <a href=""><img height="300" style="height:250px;width: 100%" src="{{$dish->image}}" alt=""/></a>
                                <span class="product-name text-uppercase  black-text" ><a style="font-weight: bold; background-color: #FFFFFF;border: 1px solid white">{{$dish->name}}</a></span>
                                <span class="price black-text">{{$dish->price}} €</span>


                                <div class="quick-buy" style="width:100px;height:100px;margin-left:150px;">
                                    <div class="product-share">
                                        @if(Session::has('manu_nav') &&  Session::get('manu_nav') == true)

                                        @else
                                            <a href="{{url('add_item_to_cart',$dish->id)}}" class="highlight-button-dark btn btn-small no-margin-right quick-buy-btn"  title=""><i class="fa fa-shopping-cart"></i></a>
                                        @endif
                                    </div>
                                </div>

                            </div>
                        </div>
                        <!-- end shop item -->
                @endforeach

【问题讨论】:

  • 没有解开你的问题..你可以编辑或更清楚吗?
  • @FaisalMehmoodAwan 嗯,现在可以了吗?

标签: php laravel session laravel-5


【解决方案1】:

在您的场景中,从控制器设置会话存在问题。它适用于当前场景,但必须删除会话。

否则,即使他们从菜单栏打开,会话也将在那里,因此添加到购物车选项也将在此处禁用,因为刀片文件检查会话。一种解决方法是通过在 index 方法中删除会话来消除此问题。

然而,laravel 有一个内置的解决方法,在会话中称为 flash data

$request->session()->flash('menu_nav', 'true');

此会话仅在第一次请求时存在,并在页面重新加载时自动重置。因此,您可以在 index_nav 方法中触发 flash 会话并使用您的刀片进行检查,当用户转到下一页时,它将自动取消设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2023-03-27
    相关资源
    最近更新 更多