mercredi 2 mars 2016

Sort data based on count with sub-relationship in Laravel 5

I have three tables, users, branches and jobs. users table has one-to-one relationship with branches table and one-to-many relationship with jobs table. Branches are responsible for uploading jobs and users table can include various categories including branch. Both branches and jobs table has user_id as a foreign key. Now I want to obtain the branches list with number of jobs uploaded by them and also sort them by highest number of uploads first.

I am able to display list of branches along with the number of jobs they posted as in the following code, but I want to sort according to the number of jobs posted. How can I achieve that?

@foreach(\App\Branch::all() as $branchesJob) 
<div class="col-lg-3 col-xs-3">
  <div class="small-box bg-olive-active">
    <div class="inner">
      <h3>{{\App\Job::where('user_id','=',$branchesJob->user->id)->count()}}</h3>
      <p>{{$branchesJob->branch_name}}</p>
    </div>
    <a class="small-box-footer" href="{{(route('admin.branch_job.show',$branchesJob->id))}}">
                                            More info
                                            <i class="fa fa-arrow-circle-right"></i>
                                        </a>
  </div>
</div>
@endforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire