samedi 10 février 2018

Counting total posts by a user in the blade view

I have sent a collection of all posts in my blog to my index view and then used the following code to count the total posts made by each user.

<p class="joined-text">Posts: </p>

Is this bad practice to do this from within the blade view? If it is how would I achieve this?

Models

class Posts extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->hasMany(Comments::class, 'post_id');
    }
}





class User extends Authenticatable
{

    public function posts()
    {
        return $this->hasMany('\App\Posts::class');
    }

    public function comments()
    {
        return $this->hasMany(Comments::class);
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire