gpt4 book ai didi

php - 如何根据表的值高亮显示记录?

转载 作者:行者123 更新时间:2023-11-29 06:38:35 25 4
gpt4 key购买 nike

我正在为我的 Web 应用程序使用 Laravel 5.6mysql。在我的应用程序中,我有名为 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 相关的广告。怎么办?

最佳答案

我不完全确定我的问题是否正确,但是仅使用 if 来确定 adtype 是否等于 1 并处理 的问题在哪里>div 的类或样式属性不同吗?

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

除此之外:您不应该在模板中延迟加载某些内容;这不是模板的目的,这是 Controller 的任务。使用eager loadingproper relation相反。

注意:您也可以 - 或应该 - type cast属性以符合其最初目的并防止误导性比较。

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

'adtype' => 'boolean',

关于php - 如何根据表的值高亮显示记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52702907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com