Home > Back-end >  Conditioning when there's no data with 'Active' status
Conditioning when there's no data with 'Active' status

Time:10-11

I have a room table which there is a field called 'status' (the value is enum: Active or Nonactive). The default value of status filed is Nonactive when I add a data in room table.

In the dashboard, I only want to show the data when status is Active. And if there's no data with status 'Active' (all is Nonactive), I want to display such a blank space with words to notify the user that the active room is still not available. So how to conditioning it in Laravel. I've try but still have no idea about this. Here the code:

<div class="card-body">
    <!-- if there's no Active room, show this -->
    <div class="p-4 text-center">
        <small class="font-italic centering-auto">Nothing</small>
    </div>
    
    <!-- if there's Active room, show this -->
    @foreach ($diklats->where('status', 'Aktif')->take(5) as $diklat_list)
    <div class="transaction-item">
        <a href="javascript:void(0)" class="text-dark">
            <div class="media">
                @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>
                <div class="media-body ml-1">
                    <h6 class="transaction-title">{{$diklat_list->name}}</h6>
                    <small>{{$diklat_list->userDiklat->where('is_approve',1)->count()}} peserta</small>
                </div>
            </div>
        </a>
        <div class="font-weight-bolder text-danger"></div>
    </div>
    @endforeach
</div>

Thank u in advance

CodePudding user response:

use Laravel eloquent count

@if($diklats->where('status', 'Aktif')->count())

to check if there are active rooms.

 <div class="card-body">
         <!-- if Active room, show this -->
            @if($diklats->where('status', 'Aktif')->count())
            @foreach ($diklats->where('status', 'Aktif')->take(5) as $diklat_list)
            <div class="transaction-item">
                <a href="javascript:void(0)" class="text-dark">
                    <div class="media">
                        @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>
                        <div class="media-body ml-1">
                            <h6 class="transaction-title">{{$diklat_list->name}}</h6>
                            <small>{{$diklat_list->userDiklat->where('is_approve',1)->count()}} peserta</small>
                        </div>
                    </div>
                </a>
                <div class="font-weight-bolder text-danger"></div>
            </div>
            @endforeach
           @else
             <!-- if no Active room, show this -->
           <div class="p-4 text-center">
            <small class="font-italic centering-auto">Nothing</small>
           </div>
    
           @endif
    </div>

CodePudding user response:

Try as like below

<div class="card-body">
    <!-- if there's no Active room, show this -->
    <div class="p-4 text-center">
        <small class="font-italic centering-auto">Nothing</small>
    </div>
    
    <!-- if there's Active room, show this -->
    $active_statuses = $diklats->where('status', 'Aktif')->take(5);
    @if(count($active_statuses) > 0 )
@foreach ( as $diklat_list)
    <div class="transaction-item">
        <a href="javascript:void(0)" class="text-dark">
            <div class="media">
@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>
                <div class="media-body ml-1">
                    <h6 class="transaction-title">{{$diklat_list->name}}</h6>
                    <small>{{$diklat_list->userDiklat->where('is_approve',1)->count()}} peserta</small>
                </div>
            </div>
        </a>
        <div class="font-weight-bolder text-danger"></div>
    </div>
@endforeach
    @else
    {{ Do your stuff here }}
    @endif
</div>

CodePudding user response:

First I think you should cleanup your blade, try not to have logic in there. I moved logic to controller and the blade will only display

In your controller


// ... controller logic

$diklat_list = $diklats->where('status', 'Aktif')->take(5)
$has_active_rooms = $diklat_list->count() > 0;

return view('template-here', compact('diklat_list', 'has_active_rooms'));

In blade

<div class="card-body">
    @if ($has_active_rooms)
        @foreach ($diklat_list as $room)
            <!-- Active room data goes here... -->    
            <h6>{{ $room->name }}</h6>
        @endforeach
    @else 
        <!-- No active rooms -->
        <div class="p-4 text-center">
            <small class="font-italic centering-auto">Nothing</small>
        </div>
    @endif
</div>
  • Related