I get two queries with views. And if posts 100, then queries will be 200.. For each post I get two same queries. How I can resolve this? If I delete {{ trans_choice('trans.views', $post->views) Then I get one query.
@DRONAX - You can use the withCount method above or if you want to use your method:
When you are doing this:
$this->views()->count()
You are using the query builder to count your views() relation:
If you want to use your accesor without doing the 100 queries you need to load the relation before using it, and using the collection count:
protected $appends = ['views'];
public function getViewsAttribute()
{
return $this->views->count(); //removed the () in views so you are working with collections instead.
}
Then your $posts variable query needs to eager load your views relation:
$posts = App\Post::with('views')->get();
And you will end up with one query using your method.