2 回答

TA贡献1806条经验 获得超5个赞
所以看起来您正在根据您想要显示的帖子通过以下关系加载所有帖子$post->belongsTo(Category::class)(这就是我想象的帖子模型的样子)
我要做的就是这个。
在我的控制器中:
class PostController extends Controller
{
public function post(post $post)
{
// eager load the categories and their posts
$post->load([
'categories',
// need to pass the selected post into the scope of the callback
'categories.posts' => function ($query) use ($post) {
// give us all the posts, besides this one, that belong to the category
$query->where('id', '!=', $post->id)
}
]);
return view('user.post', compact('post'));
}
}

TA贡献1843条经验 获得超7个赞
下面的方法通过在循环中添加 except 方法来解决该问题。
@foreach($post->categories as $category)
@foreach($category->posts->except($post->id) as $catPost)
<p>{{ $catPost->title }}</p>
<br>
@endforeach
@endforeach
- 2 回答
- 0 关注
- 118 浏览
添加回答
举报