Controller
$articles = Article::with('tags')->latest()->get();
Blade
@foreach($articles as $article)
<h2>{{ $article->title }}</h2>
@foreach($article->tags as $tag)
<span>{{ $tag->name }}</span>
@endforeach
<hr>
@endforeach
The above works. Now, I'd like to select just some columns from the models.
I have tried the following but now the names of the tags are not displayed and not getting any error.
$articles = Article::select(['title', 'created_at', 'body', 'slug'])->with(['tags' => function ($query) {
$query->select('name');
}])->latest()->get();