Flex's avatar
Level 4

How to highlight displaing values acording to values of table?

Hi, I am using Laravel 5.6 and mysql for my web application. in My application I have table name as vehicles as following,

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

and I am displaying above data on VehicleController as following

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

upload is related table witch contain images of the vehicles

and My index.blade.php is like this

<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>

it is working fine. but now I need highlight ads which related to adtype value as 1 from the vechicles table. how can do it?

0 likes
1 reply
D9705996's avatar

What do you mean by highlight? If want to change the CSS styling you can do a conditional statement in your view

@if( $vehicule->adtype == 1 )
    <div style="adtype 1 styling">
@else
    <div style="adtype 0 styling ">
@endif

However if you want the items sorted by adtype so all the 1's appear at the top e.g. a premium listing you can order your vehicles in the query

$vehicles = Vehicle::with('uploads')
  ->orderBy('adtype', 'desc') // add this (if you want 0 at the top change desc to asc)
  ->get();
1 like

Please or to participate in this conversation.