【问题标题】:Laravel 4 relationship sort one-to-many count eager loadingLaravel 4关系排序一对多计数急切加载
【发布时间】:2014-06-21 01:04:02
【问题描述】:

不确定这是否可能。我可以对当前表格艺术家(标题、正文等)中的所有内容进行排序,但还没有想出将其排序到与之相关的表格中。我想根据他/她拥有 DESC、ASC 的歌曲总数对艺术家进行排序。

如何动态排序艺术家的歌曲总数?

admin/artists?sortby=songs&order=asc

Screenshot

我的桌子

艺术家:id、名字、身体、蛞蝓

歌曲:id、title、body、slug、hits、artist_id

在此之前,我在控制器中使用了预加载,Artist::with('songs)... 然后在我可以在 foreach 循环中使用的视图中,count($artist->songs) 用于总歌曲。然后我找到了一种更清洁的方法 http://laravel.io/forum/05-03-2014-eloquent-get-count-relation

艺人模特

public function songs()
    {
        return $this->hasMany('Song');
    }

public function songsCountRelation()
{
    return $this->hasOne('Song')->selectRaw('artist_id, count("id") as count')->groupBy('artist_id');
}

public function getSongsCountAttribute()
{
    return $this->songsCountRelation->count;
}

歌曲模型

public function artist()
{
    return $this->belongsTo('Artist');
}   

控制器

public function index()
{
    // Sortby and order variables from url 
    $this->data['sortby'] = Input::get('sortby');
    $this->data['order'] = Input::get('order');

    // If the sortby and order exists in url, fetch the data and order it accordingly
    if ($this->data['sortby'] && $this->data['order'])
    {
        // Order the data based on keys from url
        $this->data['artists'] = Artist::with('songsCountRelation')->orderBy($this->data['sortby'], $this->data['order'])->get(['id', 'name', 'body', 'slug']);

    }
    else
    {
        $this->data['artists'] = Artist::with('songsCountRelation')->get(['id', 'name', 'body', 'slug']);
    }

    $this->layout->content = View::make('admin.artists.index', $this->data);
}

索引视图

<th>
    {{--Sort by slug--}}
    @if ($sortby == 'slug' && $order == 'asc')

        {{ link_to_route('admin.artists.index', 'Slug', ['sortby' => 'slug', 'order' => 'desc']) }}
    @else
        {{ link_to_route('admin.artists.index', 'Slug', ['sortby' => 'slug', 'order' => 'asc']) }}
    @endif
</th>
<th>
    {{--Sort by total songs--}}
    @if ($sortby == 'songs' && $order == 'asc')
        {{ link_to_route('admin.artists.index', 'Songs', ['sortby' => 'songs', 'order' => 'desc']) }}
    @else
        {{ link_to_route('admin.artists.index', 'Songs', ['sortby' => 'songs', 'order' => 'asc']) }}
    @endif
</th>
...¨
@foreach($artists as $artist)
    <tr>
...
        <td>{{ $artist->songsCount; }}</td>
...     
    </tr>

@endforeach

提前致谢!

【问题讨论】:

    标签: php laravel count one-to-many eloquent


    【解决方案1】:

    你必须加入表格。

    Artist::join('songs', ...)
      ->orderByRaw('count(songs.*) asc')
      ->select('artists.*')
      ->get();
    

    【讨论】:

    • 感谢您引导我走向正确的方向。以控制器中的代码结束。 $this-&gt;data['artists'] = DB::table('artists') -&gt;join('songs', 'artists.id', '=', 'songs.artist_id') -&gt;select(DB::raw('artists.*, count(*) as songs')) -&gt;groupBy('artists.id') -&gt;orderBy($this-&gt;data['sortby'], $this-&gt;data['order']) -&gt;get();
    猜你喜欢
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2017-03-01
    相关资源
    最近更新 更多