【问题标题】:Do not show if 1 variables are equal to 0 and the other one is empty in laravel不显示 1 个变量是否等于 0 而另一个变量在 laravel 中为空
【发布时间】:2019-03-17 07:45:25
【问题描述】:

我不知道为什么我不能使这个条件起作用。首先,我将向您展示可能的 foreach 表。

@foreach( $w1ProductHistory as $w1ph )
    @php
    $totalCost = $w1ph->orderItem->cost * $w1ph->stock_in_qty;
    @endphp
    @if($w1ph->stock_in_qty!='0' && $w1ph->delivery_note!='')
    <tr>
        <td>{{$w1ph->orderItem->product_code}}</td>
        <td>{{$w1ph->orderItem->product_name}}</td>
        <td><a href="{{ URL('/orders/'.$w1ph->order->id)}}" class="text-info">{{$w1ph->order->purchase_no}}</a></td>
        <td>{{$w1ph->delivery_date}}</td>
        <td>{{$w1ph->companies->comp_name}}</td> 
        <td>{{$w1ph->orderItem->cost}}</td>
        <td>{{$w1ph->stock_in_qty}}</td>
        <td>{{number_format($totalCost)}}</td>
        <td>
            @if(isset($w1ph->delivery_note))
                <a data-placement="left" data-toggle="tooltip" title="Delivery Notes: {{$w1ph->delivery_note}}" href="{{-- {{ route('orders.edit',$order->id) }} --}}" class="btn btn-sm btn-icon btn-default"><i class="fab fa-readme"></i></a>
            @endif
        </td>
    </tr>
    @endif
@endforeach

我想要做的只是隐藏 $w1ph->stock_in_qty!='0$w1ph->delivery_note!=''

如果 $w1ph->stock_in_qty!='0$w1ph->delivery_note!='' 具有价值,则应显示该行。

我已经尝试过 @if($w1ph-&gt;stock_in_qty!='0' &amp;&amp; $w1ph-&gt;delivery_note!='')@if($w1ph-&gt;stock_in_qty!='0' &amp;&amp; !empty($w1ph-&gt;delivery_note)) 甚至将“&&”更改为 and。即使只满足一个条件,它仍然会隐藏该行。

再一次,我试图隐藏该行,只有当两个条件都满足而不满足其中任何一个时。我怎样才能实现它?提前致谢!

【问题讨论】:

    标签: laravel if-statement conditional-statements


    【解决方案1】:

    在刀片文件中包含@php 标签是否完全合法,但您可能会重新考虑是否真的需要这样做。

    为什么?因为它使您的刀片文件更干净,您应该努力保持刀片文件除了获取属性/关系之外没有任何其他逻辑。 此外,如果您需要在应用程序的其他位置进行该计算,您可以在模型上进行计算,并且如果值背后的算法发生变化,您只需将其更改为一个位置。

    在我看来,更好的解决方案是在您的 ProductHistory(?) 模型上定义一个属性,该属性为您提供此值,然后执行 $w1ph->totalCost;

    我还会添加一个属性,该属性会根据您是否需要在表格中显示此项目来返回布尔值,例如 $w1ph->showInTable/visible/showDisplay 等。

    你试过没有 0 左右的引号吗?还是 count($w1ph-->stock_in_qty) == 0)?

    【讨论】:

    • 谢谢@kapalua 不,我还没有。我现在就试试。也感谢你的提示,我下次会这样做。实际上这是我第一次尝试使用 php 框架。这个提示肯定会注意到。
    【解决方案2】:

    我想你可以试试

        @if(count($w1ph->stock_in_qty)>0)&& (count($w1ph->delivery_note)>0))
    
    @endif
    

    【讨论】:

      猜你喜欢
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多