【问题标题】:How to check session value at Blade如何在 Blade 上检查会话值
【发布时间】:2021-09-16 21:43:42
【问题描述】:

我正在使用 Laravel 5.8,我想在 cart.blade.php Blade 处设置一个会话变量,如下所示:

@php
    $conflicted = '';
@endphp

@if($conflicting  && ($prd[0]->prd_delivery == 'city_free_delivery' || $prd[0]->prd_delivery == 'country_free_delivery'))
    @php Session::put($conflicted , '0') @endphp
        <p style="color:red;">
           This product has free delivery but it can not be set because of service area conflict of other products 
        </p>
@endif

@if(!$conflicting  && ($prd[0]->prd_delivery == 'country_free_delivery'))
    @php Session::put($conflicted , '1') @endphp
        <p style="color:red;">
           Country Free Delivery
        </p>
@endif
    
@if(!$conflicting  && ($prd[0]->prd_delivery == 'city_free_delivery'))
    @php Session::put($conflicted , '2') @endphp
        <p style="color:red;">
            City Free Delivery
        </p>
@endif

现在在checkout.blade.php,我需要检查$conflicted 变量的会话值。

那么如何在 Blade 上查看会话值?

因为使用get 和has 方法,我可以检查会话密钥名称。

【问题讨论】:

  • 刀片视图用于显示不设置变量以在其他地方使用的东西。这应该在控制器中完成。
  • @shaedrich 我需要在 Blade 做这件事
  • 你遇到了什么问题?
  • @shaedrich 我想通过会话获得$conflicted 值。所以我可以检查它的值是 1、2 还是 0
  • 你已经提到了,你可以通过Session::get($key)获得它们。你需要我们做什么?或者,您可以分别使用session($key) 或session()-&gt;get($key)。

标签: php laravel session laravel-5.8 laravel-session


【解决方案1】:

你可以使用session()助手:

@if (session('conflicted'))
    {{ session('conflicted') }}
@endif

【讨论】:

  • 检查的值在哪里 conflicted 等于 1 或 2 或 0
  • 您可以使用if语句检查该值:@if (session('conflicted') == 1)
  • 我们也可以说:if($request-&gt;session('conflicted') == '1') 吗?
猜你喜欢
  • 2021-02-11
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 2012-02-26
相关资源
最近更新 更多