繁体   English   中英

Laravel是并且不是空值检查

[英]Laravel is and is not empty value check

我知道我们可以检查值是否为空,例如:

@if(!empty(test))
//print results
@endif

但是如果我们要交叉检查2列为空而不为空怎么办?

样品:

我想检查一下: $test->one$test->two

  1. 如果$test->one 不为空并且$test->two 为空 = x
  2. 如果$test->one$test->two 都不为空 = y

我怎么做?

更新

我的代码:

PS:

  1. $test->one我的样本中的一个等于$order->options_price
  2. $test->two示例中的$test->two等于$order->shipment_price

    @if(!empty($ order-> product_data))

      {{ number_format($order->total_price(), 0) }} @elseif(!empty($order->options_price) && empty($order->shipment_price)) {{ number_format($order->quantity * $order->price + $order->options_price, 0) }} @elseif(!empty($order->options_price) && !empty($order->shipment_price)) {{ number_format($order->quantity * $order->price + $order->options_price + $order->shipment, 0) }} @else {{ number_format($order->quantity * $order->price, 0) }} @endif 

问题是我无法获得shipment_price

固定:

我的代码中确实有键入错误。

你可以做

如果$ test-> one不为空并且$ test-> two为空

@if(!empty($test->one) && empty($test->two))
//print results
@endif

如果$ test-> one和$ test-> two都不为空

@if(!empty($test->one) && !empty($test->two))
//print results
@endif

Laravel还提供了blank助手方法。

来自文档的示例:

blank('');
blank('   ');
blank(null);
blank(collect());

// true

blank(0);
blank(true);
blank(false);

// false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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