dox90448 2018-06-13 15:57
浏览 322
已采纳

在Laravel中按计数排序

In my Laravel application, I have implemented Tagging of articles and events. This is accomplished by having two tables: tags and taggables. In my tag controller, I have an index method that grabs all the tags, a count of the tags and the same logic to count used tags.

It looks like this:

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $tags = Tag::orderByDesc('name')->get();    
    $tagCount = count($tags);
    $usedTags = Tag::has('articles')->orHas('events')->orderBy('name')->get();
    $usedTagsCount = count($usedTags);

    return view('pages.tags.index', compact('tags', 'usedTags', 'tagCount', 'usedTagsCount'));
}

I pass all this data to my view which looks like this:

<div class="container">
    <div class="row">

        <!-- Search results -->
        <div class="col-md-8">
            <div class="padded-content-box">
                <div class="header">
                    <div class="row">
                        <div class="col-xs-12 col-md-12">
                            <h1 class="heading">Tags</h1>
                        </div>
                    </div>
                </div>
            </div>

            <div class="padded-content-box">
                    <div class="header">
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <h2 class="sub-heading">
                                    All Tags ({{ $tagCount }})
                                </h2>
                            </div>
                        </div>
                    </div>

                    <div class="content">
                        <div class="tag-row">
                            <div class="tag-words-list">
                                @foreach($tags as $tag)
                                    <a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController@show', $tag->slug) }}">{{ $tag->name }}</a>
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div>

            <div class="padded-content-box">
                    <div class="header">
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <h2 class="sub-heading">
                                    Tags currently in use ({{ $usedTagsCount }})
                                </h2>
                            </div>
                        </div>
                    </div>

                    <div class="content">
                        @foreach($usedTags as $tag)
                        <p>
                            <a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController@show', $tag->slug) }}">{{ $tag->name }}</a> 
                            x {{  count($tag->articles) + count($tag->events) }}
                        </p>
                        @endforeach
                    </div>
                </div>
        </div>

        <div class="col-md-4"></div>
    </div>
</div>

As you can see, to show a count of tags used I just add the articles and events that have a given tag.

How would I count how many times each tag is used in both events and articles, then order them by the count. Essentially if a tag has been used the most it would be at the top of a list.

Update

As suggested I have changed my controller method to look like this:

public function index()
{
    $tags = Tag::orderBy('name')->get();

    $tagCount = count($tags);

    $usedTags = Tag::has('articles')->orHas('events')->withCount('articles', 'events')->orderByRaw('articles_count + events_count DESC')->orderBy('name')->get();

    $usedTagsCount = count($usedTags);

    return view('pages.tags.index', compact('tags', 'usedTags', 'tagCount', 'usedTagsCount'));
}

This ensures that the tags are in count and alphabetical order.

Then in my view I changed the count code to this:

<p>
    <a class="tag-word" title="{{ $tag->name }}" href="{{ URL::action('TagController@show', $tag->slug) }}">{{ $tag->name }}</a> x {{  $tag->articles_count + $tag->events_count }} 
</p>

This way I get to remove some minor logic from the view.

  • 写回答

1条回答 默认 最新

  • dpqvwgr74759 2018-06-13 20:31
    关注

    Use withCount():

    $tags = Tag::withCount('articles', 'events')
        ->orderByRaw('articles_count + events_count DESC')
        ->get();  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算