lundi 25 juin 2018

Return most recent value of database for each server, and not change position of the server within the list.

Currently I have a laravel project, in which I am returning server statuses. What I am trying to do is call my database tables, in a join statement. get the most recent status for each server and update the view accordingly.

I previously had it working doing two different database calls, Where I ordered servers by created at date and did a call returning all statuses. I then returned this to the view and iterated through both collections and displayed the information that way, shown below.

@if(count($servers) > 0)
    @foreach($servers as $server)
        @foreach ($server->serverStatus as $status)
            @if($loop->last)
                @if($status->status_id == 1)
                    <a href="/servers/"  class="text-dark list-group-item list-group-item-success"></a>
                @elseif($status->status_id == 2)
                    <a href="/servers/" class="text-dark list-group-item list-group-item-warning" ></a>
                @elseif($status->status_id == 3)
                    <a href="/servers/" class="text-dark list-group-item list-group-item-danger" ></a>
                @elseif($status->status_id == 4)
                    <a href="/servers/" class="text-dark list-group-item list-group-item-warning" > is under maintinance</a>
                @endif
            @endif
        @endforeach
    @endforeach
@endif

What I have tried so far is doing a left join with servers and statuses, ordered by the status created at date, like so.

$servers = Server::leftjoin('server_statuses', 'servers.id' , '=' , 'server_statuses.server_id')
                        ->orderBY('server_statuses.created_at', 'desc')
                        ->get();

This shows all of the statuses in the order they were created at in my view though and it also repeatsthe servers.

@if(count($servers) > 0)
    @foreach($servers as $server)
            @if($server->status_id == 1)
                <a href="/servers/"  class="text-dark list-group-item list-group-item-success"></a>
            @elseif($server->status_id == 2)
                <a href="/servers/" class="text-dark list-group-item list-group-item-warning" ></a>
            @elseif($server->status_id == 3)
                <a href="/servers/" class="text-dark list-group-item list-group-item-danger" ></a>
            @elseif($server->status_id == 4)
                <a href="/servers/" class="text-dark list-group-item list-group-item-warning" > is under maintinance</a>
            @endif
    @endforeach
@endif

enter image description here

As you can see from the photo, servers are repeated as the statuses are updated. I just cant seem to figure out how to limit it so that the server inst repeated and the order of the server doesn't change within the lsit.

Any help would be much appreciated, and thank you in advance!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KhhnIv
via IFTTT

Aucun commentaire:

Enregistrer un commentaire