显示每个产品的比较属性

时间:2018-09-26 14:09:29

标签: php laravel

我有一个代码:

<table class="table table-bordered">
            <tr>
              <td class="align-middle">
            
              </td>
              @forelse($products as $product)
                <td>
                  <div class="comparison-item">
                    <a class="comparison-item-thumb" href="shop-single.html">
                      <img src="{{ Voyager::image($product->image) }}" alt="Apple iPhone X">
                    </a>
                    <a class="comparison-item-title" href="shop-single.html">{{ $product->title }}</a>
                  </div>
                </td>
                
              @empty

              Нет товаров для сравнения

              @endforelse
            </tr>

            @foreach($products as $keyp => $product)
            
                @foreach($product->prodChar as $key => $char)
                <tr>
                    
                    <th>{{ $char->prodCharacter->title }}</th>
                    <td>{{ $char->value }}</td>
                          
                    <td>-</td>
                    <td>-</td>
                
                </tr>
                @endforeach
            
            @endforeach
          
            <tr>
              <th></th>
              <td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
              <td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
              <td><a class="btn btn-outline-primary btn-sm btn-block" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-check-circle" data-toast-title="Product" data-toast-message="successfuly added to cart!">Add to Cart</a></td>
            </tr>
        </table>

图片显示方式:

Click

我需要写:"-"。对于属性为none的产品。现在,我在每种产品的一栏中显示所有属性。我该如何解决这个问题?我需要比较数组的键吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么您在这里谈论的是这一小节:

           @foreach($product->prodChar as $key => $char)
            <tr>

                <th>{{ $char->prodCharacter->title }}</th>
                <td>{{ $char->value }}</td>

                <td>-</td>
                <td>-</td>

            </tr>
            @endforeach

所以您在这里有两个选择:

选项1 :您可以在视图中进行如下比较:

<td>{{ $char->value?: "-" }}</td>

只要您的值为空,这将返回-

选项#2 :您可以在ProductChar模型中创建一个变体,如下所示:

 // ...your product model code
 public function getValueAttribute() {
  return $this->attributes['value']?: "-";
 }

这样做可以使您在调用$char->value时将使用该值(如果不为null)或返回一个-(如果为null)。