daijiro of Techpit
3/12/2020 - 9:02 AM

Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Article extends Model
{
    // 略
    public function getCountLikesAttribute(): int
    {
        return $this->likes->count();
    }

    //===========ここから追加===========
    public function tags(): BelongsToMany
    {
        return $this->belongsToMany('App\Tag')->withTimestamps();
    }
    //===========ここまで追加===========
}