Ad

Laravel Project Panels Orderby On Their Priority Won't Work

- 1 answer

So stackoverflow,

I am currently developing an helpdesk project. And I need to implement code so the projects shown on the dashboard are ordered by their related bug priority. I have made an Eloquent relationship but I can't get the orderby working.

NOTE The colors aren't shown with the orderby code they appear grey..enter image description here

That's what I am seeing.

when I use the : $projects = Project::with('bug')->get(); code instead of the one with the case.

But the order I want is : purple >red >green

My controller code :

public function showDashboard()
{
    $klant_id = Auth::user()->id;
    $bugs = Bug::all();
    $bugs_send = Bug::where('klant_id' , '=', $klant_id)->get();
    $projects = Project::with(['bug' => function($query){
        $query->orderByRaw('
        CASE
            WHEN prioriteit = "kritisch"   THEN 4
            WHEN prioriteit = "hoog"       THEN 3
            WHEN prioriteit = "gemiddeld"  THEN 2
            ELSE 1
        END
        ','DESC');
    }])->get();
    $projects_send = Project::where('gebruiker_id', '=', $klant_id)->get();
    if(\Auth::guest()){
        return redirect('/');
    }
    else if(\Auth::user()->bedrijf == 'moodles'){
        return View::make('/admindashboard', compact('bugs','projects'));
    }else{
        return View::make('/dashboard', compact('bugs_send','projects_send'));
    }
}

My code for showing the project panels :

<div class="row">
            @if(count($projects) > 0)
            @foreach($projects as $pro)
            {{-- */$ont=0;/* --}}
             {{-- */$i=0;/* --}}
             {{-- */$x=0;/* --}}
             {{-- */$y=0;/* --}}
             {{-- */$krit=0;/* --}}
             {{-- */$hoog=0;/* --}}
             {{-- */$gem=0;/* --}}
             {{-- */$laag=0;/* --}}
             {{-- */$crit = '';/* --}}
             {{-- */$high = '';/* --}}
             {{-- */$avg = '';/* --}}
             {{-- */$low = '';/* --}}
             {{-- */$panel_type = '';/* --}}
            <div class="col-lg-2 col-md-6">

            @foreach($pro->bug as $bug)


            @if($bug->prioriteit == 'kritisch' && $bug->status != 'gesloten')
            {{-- */$crit='kritisch';/* --}}
            @elseif($bug->prioriteit == 'hoog' && $bug->status != 'gesloten')
            {{-- */$high='hoog';/* --}}
            @elseif($bug->prioriteit == 'gemiddeld' && $bug->status != 'gesloten')
            {{-- */$avg='gemiddeld';/* --}}
            @elseif($bug->prioriteit == 'laag' && $bug->status != 'gesloten')
            {{-- */$low='laag';/* --}}
            @else
            @endif
            @endforeach


            @if($crit == 'kritisch')
                   {{-- */$panel_type='purple';/* --}}
               @elseif($high == 'hoog')
                   {{-- */$panel_type='red';/* --}}
               @elseif($avg == 'gemiddeld')
                   {{-- */$panel_type='yellow';/* --}}
               @elseif($low == 'laag')
                   {{-- */$panel_type='green';/* --}}
                   @else
                   {{-- */$panel_type='default';/* --}}
               @endif

                    <div class="panel panel-{{$panel_type}}">
                    <div class="panel-heading" style="padding-left:10px;padding-right:10px;">
                    <a target="_blank" rel="nofollow noreferrer" href="/bugs/{{$pro->id}}">
                        <div class="row">
                            @foreach($bugs as $bug)
                            @if($bug->medewerker_id < 1)
                            @if($bug->project_id == $pro->id)
                            {{-- */$ont++;/* --}}
                            @endif
                            @endif
                            @if($bug->prioriteit == 'laag')
                                @if($bug->project_id == $pro->id)
                                {{-- */$laag++;/* --}}
                                @endif
                            @endif
                            @if($bug->prioriteit == 'gemiddeld')
                                @if($bug->project_id == $pro->id)
                                {{-- */$gem++;/* --}}
                                @endif
                            @endif
                            @if($bug->prioriteit == 'hoog')
                                @if($bug->project_id == $pro->id)
                                {{-- */$hoog++;/* --}}
                                @endif
                            @endif
                            @if($bug->prioriteit == 'kritisch')
                                @if($bug->project_id == $pro->id)
                                {{-- */$krit++;/* --}}
                                @endif
                            @endif
                            @endforeach
                            <div id='notificatie'><div>
                            {{$ont}}
                            </div></div>
                            <div class="col-xs-12 text-right pull-right">
                            <span style="border: solid #ffffff 1px;" class="label label-purple pull-left">{{$krit}}</span>

                                <small><strong>{{substr($pro->projectnaam,0,15)}}..</strong></small>
                                <div>
                                <span style="border: solid #ffffff 1px;" class="label label-danger pull-left">{{$hoog}}</span><span class="badge">
                                @foreach($bugs as $bug)
                                @if($bug->status == 'open')
                                    @if($bug->project_id == $pro->id )
                                        {{-- */$i++/* --}}
                                    @endif
                                @endif
                                @endforeach
                                {{$i}}
                                </span> Openstaand</div>
                                <span style="border: solid #ffffff 1px;" class="label label-warning pull-left">{{$gem}}</span>
                                <div><span class="badge">
                                @foreach($bugs as $bug)
                                @if($bug->status == 'bezig')
                                    @if($bug->project_id == $pro->id)
                                        {{-- */$x++/* --}}
                                    @endif
                                @endif
                                @endforeach
                                {{$x}}
                                </span> Bezig</div>
                                <span style="border: solid #ffffff 1px;" class="label label-success pull-left">{{$laag}}</span>
                                <div><span class="badge">
                                @foreach($bugs as $bug)
                                @if($bug->status == 'gesloten')
                                    @if($bug->project_id == $pro->id)
                                        {{-- */$y++/* --}}
                                    @endif
                                @endif
                                @endforeach
                                {{$y}}
                                </span> Gesloten</div>
                            </div>
                        </div>
                        </a>
                    </div>
                     <a target="_blank" rel="nofollow noreferrer" href="/bugs/{{$pro->id}}">
                        <div class="panel-footer">
                            <span class="pull-left">Bekijk</span>
                            <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
                            <div class="clearfix"></div>
                        </div>
                    </a>

                </div>

                </div>

            @endforeach
              @endif
              </div>
Ad

Answer

$temp_projects = Project::with('bug')->get();
        $projects = array();

        foreach ($temp_projects as $project) {
            $prio = 0;
            foreach ($project->bug as $bug){
                if ($bug->prioriteit > $prio && $bug->status != 'gesloten'){
                    $prio = $bug->prioriteit;
                }
            }
            $projects[$prio][] = $project;
        }

        krsort($projects);
        $temp_projects = $projects;
        $projects = array();
        foreach($temp_projects as $priority){
            foreach ($priority as $project){
                $projects[] = $project;
            }
        }

        return View::make('/admindashboard', compact('bugs','projects'));

Fixed it.

Ad
source: stackoverflow.com
Ad