Php 根据laravel中的相同类别制作“相关”部分
我有一个网站,我张贴食谱。每一篇文章都有一个类别,我想再显示2-3篇与该文章类别相同的文章。 如何生成查询以显示它?我有一个Post模型和Category模型,两者之间有一个归属关系,它填充了一个透视表,我将这些类别绑定到一个Post 这是我的BController中的函数,它将数据传递给用户可以访问和查看的视图Php 根据laravel中的相同类别制作“相关”部分,php,mysql,laravel,laravel-5.3,Php,Mysql,Laravel,Laravel 5.3,我有一个网站,我张贴食谱。每一篇文章都有一个类别,我想再显示2-3篇与该文章类别相同的文章。 如何生成查询以显示它?我有一个Post模型和Category模型,两者之间有一个归属关系,它填充了一个透视表,我将这些类别绑定到一个Post 这是我的BController中的函数,它将数据传递给用户可以访问和查看的视图 public function slug(Request $request, $slug) { if (Auth::check()) {
public function slug(Request $request, $slug)
{
if (Auth::check()) {
$fav = DB::table('post_user')->whereUserId(Auth::id())->pluck('post_id')->all();
}
/*get search query from slug page*/
$query=$request->get('q');
/*display the results of the search*/
if ($query) {
$posts = $query ? Post::search($query)
->orderBy('id','desc')
->paginate(7) : Post::all();
return view('home', compact('posts','fav'));
}
/*the $url veriable is for share buttons*/
else {
$url = $request->url();
$post = Post::where('slug', '=', $slug)->first();
return view('b.s', compact('post','url'));
}
}
这是我的帖子模型:
这是在类别模型中:
透视表如下所示:
一个可能的解决方案是包含一段代码以获得所需的类别 如果我正确理解你的模型,你可以有几个类别。所以,我们需要对你的帖子进行分类,只保留id;我们必须排除当前对象的Post ID:
$post = Post::where('slug', '=', $slug)->first();
// get Category IDs. There are others ways to do it.
$categoriesId = [];
foreach( $post->categories as $category ) {
$categoriesId[] = $cateogyr->id;
}
// get similar posts
$others = Post::
whereIn('categories', $categoriesId)
->where('id', '!=', $post->id)
->take(3)
->get();
在本例中,您有一个透视表:
$others = Post::
with(array('YourPivot' => function($query) use($categoriesId)
{
whereIn('categories', $categoriesId)
})
->where('id', '!=', $post->id)
->take(3)
->get();
您可以使用whereHas在相关表上添加约束,如下所示:
// get the post usnig slug
$post = Post::where('slug', '=', $slug)->first();
// get the related categories id of the $post
$related_category_ids = $post->categories()->pluck('categories.id');
// get the related post of the categories $related_category_ids
$related_posts = Post::whereHas('categories', function ($q) use($related_category_ids) {
$q->whereIn('category_id', $related_category_ids)
})
->where('id', '<>', $post->id)
->take(3)
->get();
你能解释一下法语吗?哦,对不起。我不知道我为什么用法语写作。我将翻译^^^谢谢,但我得到了这个错误:调用undefined方法illumb\Database\Query\Builder::AndWhere删除and',并使用where。现在我得到了这个错误,因为我的posts表中没有类别字段,它位于pivot表中,就像我在OP.SQLSTATE[42S22]中说的那样:未找到列:1054“where子句”中的未知列“categories”SQL:select*from posts where categories in 10 and id!=33.限制3。我能做什么?你的密码给我这个:集合{247▼ 项目:数组:1[▼ 0=>12]}我如何使用此选项访问实际帖子?对不起,我在这里是个傻瓜,但我是。几天前开始的,当然还在学习。谢谢。你在哪一行得到这个?我在做dd$相关的\u类别\u ID时得到它;没错。尝试下一个查询,你会得到相关的帖子。很高兴我能帮助你。你可以问一个不同的问题,我们也可以在这方面提供帮助。
$post = Post::where('slug', '=', $slug)->first();
// get Category IDs. There are others ways to do it.
$categoriesId = [];
foreach( $post->categories as $category ) {
$categoriesId[] = $cateogyr->id;
}
// get similar posts
$others = Post::
whereIn('categories', $categoriesId)
->where('id', '!=', $post->id)
->take(3)
->get();
$others = Post::
with(array('YourPivot' => function($query) use($categoriesId)
{
whereIn('categories', $categoriesId)
})
->where('id', '!=', $post->id)
->take(3)
->get();
// get the post usnig slug
$post = Post::where('slug', '=', $slug)->first();
// get the related categories id of the $post
$related_category_ids = $post->categories()->pluck('categories.id');
// get the related post of the categories $related_category_ids
$related_posts = Post::whereHas('categories', function ($q) use($related_category_ids) {
$q->whereIn('category_id', $related_category_ids)
})
->where('id', '<>', $post->id)
->take(3)
->get();
@foreach ($related_posts as $related_post)
<li>{{ related_post->title }}</li>
@endforeach