Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

449
Vistas
Change bootstrap color class every loop in Laravel (blade)

I have a limited data list (only displaying the top 5). I'd look like this :

enter image description here

And the problem is how to make the icon in different color using Bootstrap class. I want the icon in that list have 5 color like bg-light-primary for the first, bg-light-danger for the second, bg-light-success for the third, etc. The way I display the data is using Laravel @foreach. Like this:

@foreach ($diklats->where('status', 'Active')->take(5) as $diklat_list)
    <div class="transaction-item">
        <a href="javascript:void(0)" class="text-dark">
            <div class="media">
                <div class="avatar bg-light-primary rounded">
                    <div class="avatar-content">
                        <i data-feather="book" class="avatar-icon font-medium-3"></i>
                    </div>
                </div>
                <div class="media-body ml-1">
                    <h6 class="transaction-title">{{$diklat_list->name}}</h6>
                    <small>{{$diklat_list->userDiklat->where('is_approve',1)->count()}} </small>
                </div>
            </div>
        </a>
    <div class="font-weight-bolder text-danger"></div>
</div>
@endforeach

In this is the controller:

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\{Diklat, User, UserDiklat, Regency};
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    public function index()
    {
        $diklat = Diklat::where('status', 'Active')->count();
        $participant = User::where('is_participant', 1)->count();
        $regency = Regency::all()->count();
        $diklats = Diklat::with('userDiklat')->get();

        return view('admin.index', compact('diklats', 'diklat', 'participant', 'regency'));
    }
}

I have search for this case but still didn't get it and I'm in the learning stage. Would you help me, please? Thank you in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Solved with this:

  @php ($icons = ["bg-light-primary","bg-light-success","bg-light-danger","bg-light-warning", "bg-light-info"])
  <div class="avatar {{$icons[$loop->index]}} rounded">
    <div class="avatar-content">
      <i data-feather="book" class="avatar-icon font-medium-3"></i>
    </div>
  </div>
about 4 years ago · Juan Pablo Isaza Denunciar

0

Create an array of your values and use a random value from that array to use that as a class.

<?php $class_array = array("bg-primary","bg-danger","bg-success","bg-warning"); ?>
<div class="avatar-content">
    <i data-feather="book" class="avatar-icon font-medium-3 {{$class_array[array_rand($class_array)]}}"></i>
</div>

It is recommended that you should not use random class as it might repeat or all of them could be same at a time.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda