首页 > 解决方案 > 获取最大值和最小值,如果值相同,则只打印一个值,Laravel Blade

问题描述

从数组中获取最大值和最小值,如果值相同,则只打印一个值。错误:我在 foreach 中获取所有价格值,例如,Rs 120 120 120 120 120 120

<?php 
$maxprice = $product_attributes->max('price');
$minprice = $product_attributes->min('price'); ?>
  @foreach($product_attributes->sortBy('price') as $attribute)
    @if($maxprice === $minprice )
        {{ $attribute->price }}        
    @endif
  @endforeach

标签: phplaravellaravel-blade

解决方案


如果您需要从收藏中找到max并重视。min

$max = $product_attributes->max('price');
$min = $product_attributes->min('price');

@foreach($product_attributes->sortBy('price') as $attribute)
    @if($max === $min )
        {{ $attribute->price }}        
    @endif
@endforeach

推荐阅读