mercredi 13 juin 2018

Ordering by count in 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:

@extends('layouts.app') 

@section('title', 'Tags') 

@section ('content')

<!-- Breadcrumbs -->
<div class="container">
    <div class="row">

        <div class="col-xs-12">

            <div class="breadcrumb-bar">

                <ul>
                    <li>
                        <a href="">Home</a>
                    </li>
                    <li>Tags</li>
                </ul>

            </div>
        </div>

    </div>
</div>

<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>

                            <p>Tags help to categorise your content, below you'll see the tags we currently have on the Intranet. Adding a tag is easy: just add the tag when you next create an article or event.</p>

                        </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 ()
                                </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="" href=""></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 ()
                                </h2>

                            </div>

                        </div>

                    </div>

                    <div class="content">

                        @foreach($usedTags as $tag)

                        <p>
                            <a class="tag-word" title="" href=""></a> 
                            x 
                        </p>
                        @endforeach

                    </div>

                </div>



        </div>


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

        </div>

    </div>
</div>

@endsection

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

Is there a cleaner way to do this so that I could order the tags by most used?



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

Aucun commentaire:

Enregistrer un commentaire