Alexander Solonik
Alexander Solonik

Reputation: 10230

Laravel - Display number of articles with a specific tag

I have the following foreach loop that outputs all the tag names:

<section class="popular-tags-section">
    <ul class="popular-tags-listings">
        @foreach($tags as $tag)  
          <li><a href="">{{ $tag->tag }}(3)</a></li>
        @endForEach   
    </ul>
</section>

The above code gives me the following in the frontend:

tags

Now instead of the 3 which is as of now a static number, i would like to display the number of articles with that given tag.

So of now my database is such that the blog articles are stored in separate table and the tags are stored in a separate table. The blog articles table has a column called tag and the tags table has a column called tag too. Now both of these tables are passed to the view in the form of the variables:

$recentPost // Contains all the blog articles
$tag // Contains all the tags

Now how do i display dynamically the number of articles that are tagged javascript for example ?

ADDITIONAL INFO

Blog Articles table:

enter image description here

Tags Table:

enter image description here

Homepage Code:

class PagesController extends Controller {

    public function index() {
        // Grap the latest post .. the last 10 :)
        $recentPost = Admin::orderBy('created_at' , 'desc')->take(10)->get();
        $tags = Tags::all();;
        return view('pages.index')->with('recentPost' , $recentPost)->with('tags' , $tags );
    }

}

Blog articles Modal:

class Admin extends Model {

    public $table = "admin";
    // public $timestamps = false;
    protected $fillable = [
        'title',
        'description',
        'keywords',
        'blog_content',
        'tag',
        'slug',
        'filePath'
    ];

}

Tags Modal:

class Tags extends Model {
    public $table = "tags";
    public $timestamps = false;
    protected $fillable = ['tag'];
}

Upvotes: 1

Views: 649

Answers (2)

imrealashu
imrealashu

Reputation: 5099

class Tags extends Model {
public $table = "tags";
public $timestamps = false;
protected $fillable = ['tag'];

public function posts()
{
    return $this->belongsToMany(Post::class);
}}

In you controller you'd do something like this.

Tag::withCount('posts')->get();

I'm not quite sure if it would do the work or not because I'm not quite sure of the structure. Just let me know if you're get any error.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163778

Get tags with counted articles for every tag using withCount():

$tags = Tags::withCount('articles')->get();

If you want to count the number of results from a relationship without actually loading them you may use the withCount method, which will place a {relation}_count column on your resulting models

Display number of articles for specified tag:

{{ $tag->tag }} ({{ $tag->articles_count }})

This will work with correctly defined articles relationship in the Tag model. The best choice is many-to-many here, so you must have a pivot table and articles relation defined like this:

public function articles()
{
    return $this->belongsToMany(Article::class);
}

Upvotes: 2

Related Questions