【问题标题】:How to highlight displaying records acording to values of table?如何根据表的值突出显示记录?
【发布时间】:2018-10-08 22:58:49
【问题描述】:

我将Laravel 5.6mysql 用于我的Web 应用程序。在我的应用程序中,我有一个名为 vehicles 的表,如下所示,

id   name   number   adtype   
----------------------------
1   car     123     1
2   van     256     0
3   car     248     0
4   van     159     1
5   car     158     1
etc

我在VehicleController 上显示上述数据,如下所示

public function index(){
    $vehicles = Vehicle::with('uploads')->get();
    return view('vehicles.index')->withVehicles($vehicles);
}

上传的是包含车辆图像的相关表格

而我的index.blade.php是这样的

<div class="col-md-7 col-md-offset-1">
 @forelse( $vehicles as $vehicule )
     @if( $vehicule->uploads->count() > 0 )
     <a href="{{ route('vehicles.show', $vehicule->id) }}">
     @php
     $upload = $vehicule->uploads->sortByDesc('id')->first();
     @endphp
     <div style="border-style: solid; color: black; ">
         <img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
     @endif
</div>

它工作正常。但现在我需要突出显示与 vechicles 表中的 adtype 值为 1 相关的广告。怎么办?

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    我不完全确定我的问题是否正确,但是仅使用 if 来确定 adtype 是否等于 1 并以不同方式处理 div 的类或样式属性有什么问题?

    <div style="color: {{ $vehicule->adtype === 1 ? 'black' : 'blue' }}">
    

    除此之外:您不应该在模板中延迟加载某些内容;这不是模板的目的,这是控制器的任务。将eager loadingproper relation 一起使用。

    注意:您可以 - 或应该 - 还 type cast 属性以等同于它们的原始用途并防止误导性比较。

    在您的情况下,adtype 看起来像一个布尔值,因此您可以将其添加到模型的 $cast 属性中:

    'adtype' => 'boolean',
    

    【讨论】:

      【解决方案2】:

      试试

       <div class="col-md-7 col-md-offset-1">
          @forelse( $vehicles as $vehicule )
      
      
          @if( $vehicule->uploads->count() > 0 )
          <a href="{{ route('vehicles.show', $vehicule->id) }}">
              @php
              $upload = $vehicule->uploads->sortByDesc('id')->first();
              @endphp
              @if($vehicule->adtype == 1)
              <div style="border-style: solid; color: black; ">
                  <img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
              </div>
               @else
              <div style="border-style: solid; color: blue; ">
                  <img src="/images/{{ $upload->resized_name }}" height="150" width="250"></a>
              </div>
              @endif
          @endif
      </div>
      

      【讨论】:

      • 发生此错误消息 语法错误,意外'endif' (T_ENDIF)
      • 现在得到这个语法错误,文件意外结束
      • 检查你的结束div div 应该有结束元素
      • 缺少结束标签不会产生语法错误。
      • 复制整个块再试一次
      【解决方案3】:

      我不确定我是否理解你所有的代码,但你会想要这样的东西:

      在你的 CSS 中:

      <style>
      .highlight {
         ... whatever ...
      }
      </style>
      

      然后,在你的刀片中,

      @if( $vehicule->adtype == 1 )
      <span class="highlight"> ...... whatever ..... </span>
      @endif
      

      或者,如果你想内联,

      <span class="{{ ($vehicule->adtype == 1) ? 'highlight' : '' }}">
      

      【讨论】:

        猜你喜欢
        • 2021-07-31
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 2021-07-03
        • 2011-12-01
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多