如何在 OctoberCMS 中列出相关博文
How to list related blog posts in OctoberCMS
我正在使用 OctoberCMS 构建应用程序,并希望根据类别在每个 post 页面的底部列出相关博客 post。我发现 rainlab_blog_posts table 没有指向博客类别 table 的外键。为了实现我想要的,我想到扩展 blog_posts table 并使用插件添加 category_id 作为外键。我在 table 迁移中定义了外键约束。一切似乎都很好。我的挑战是,每次创建新的 post 时,如何在 posts table 中插入类别 ID。 OctoberCMS create post 后端有一个选项,作者可以通过从列表中选择来为新博客 post 分配一个类别。只是不明白如何传递此类别 ID 并插入 rainlab_blog_posts table 中的 category_id 字段。
这就是我想在 API 中实现的目标:
routes.php
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}',function($id)
{
$post = Post::where('id',$id)->first();
$posts = Post::where('category_id',$post->category_id)
->where('id','!=',$id)
->orderBy('views','desc')
->get()->take(5);
return $posts;
});
或者如果有更好的方法来实现这一点,我们将不胜感激。干杯!
嗯,这里似乎有问题,
首先我从 Blog Plugin
可以看出 Blog <=> Category
是 MM 关系,所以你 不是 在 rainlab_blog_posts
table 中找到 category_id
,因为 all relation
由 维护 mm-relation
table rainlab_blog_posts_categories
.
所以我想你会 select only one category for blog
所以你可以 get related blogs
只针对那个类别。 [根据您的代码和描述假设]
we can utilize relationships for that.
所以你的代码可以像这样
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}', function($id)
{
$post = Post::where('id',$id)->first();
// we need this because there will be mm relation so
// we fetch first category and this will be based on
// [ name sorting - does not matter as there will be only one cat. ]
$firstCategory = $post->categories()->first();
// now we fetch only that post which are related to that category
// but we skip current post and sort them and pick 5 posts
$posts = $firstCategory->posts()
->where('id', '!=', $id)
->orderBy('views','desc')
->limit(5) // use limit instead of take()
->get();
// use limit instead of take() as we don't need extra data so
// just put limit in sql rather fetching it from db then
// putting limit by code - limit() is more optimised way
return $posts;
});
Good thing now you don't need to add that category_id
field on rainlab_blog_posts
. So, I guess now you Also don't need to worry about adding it during post insertion.
如有疑问请评论。
如果您只是想显示相关博客 post 而无需自定义博客数据库,可以使用名为 Related Articles 的 OctoberCMS 插件
这需要 rainlab 博客插件。它将显示属于 post 类别的所有其他文章。
- 在您的后端菜单上,转到设置,在系统上,转到更新和插件。
- 转到安装插件。搜索插件相关文章。创建者是 Tallpro。安装它。
- 转到内容管理系统。在您想要相关文章的页面上,转到组件并拖动相关文章。保存并预览。您还可以分叉该组件以编辑您希望相关文章的显示方式。您将在文档中找到更多信息。
如果您遇到任何问题,请随时咨询,或者您可以查看插件评论以了解所面临的问题及其解决方案。
我正在使用 OctoberCMS 构建应用程序,并希望根据类别在每个 post 页面的底部列出相关博客 post。我发现 rainlab_blog_posts table 没有指向博客类别 table 的外键。为了实现我想要的,我想到扩展 blog_posts table 并使用插件添加 category_id 作为外键。我在 table 迁移中定义了外键约束。一切似乎都很好。我的挑战是,每次创建新的 post 时,如何在 posts table 中插入类别 ID。 OctoberCMS create post 后端有一个选项,作者可以通过从列表中选择来为新博客 post 分配一个类别。只是不明白如何传递此类别 ID 并插入 rainlab_blog_posts table 中的 category_id 字段。
这就是我想在 API 中实现的目标:
routes.php
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}',function($id)
{
$post = Post::where('id',$id)->first();
$posts = Post::where('category_id',$post->category_id)
->where('id','!=',$id)
->orderBy('views','desc')
->get()->take(5);
return $posts;
});
或者如果有更好的方法来实现这一点,我们将不胜感激。干杯!
嗯,这里似乎有问题,
首先我从 Blog Plugin
可以看出 Blog <=> Category
是 MM 关系,所以你 不是 在 rainlab_blog_posts
table 中找到 category_id
,因为 all relation
由 维护 mm-relation
table rainlab_blog_posts_categories
.
所以我想你会 select only one category for blog
所以你可以 get related blogs
只针对那个类别。 [根据您的代码和描述假设]
we can utilize relationships for that.
所以你的代码可以像这样
use Rainlab\Blog\Models\Post;
Route::get('apiv1/related-posts/{postid}', function($id)
{
$post = Post::where('id',$id)->first();
// we need this because there will be mm relation so
// we fetch first category and this will be based on
// [ name sorting - does not matter as there will be only one cat. ]
$firstCategory = $post->categories()->first();
// now we fetch only that post which are related to that category
// but we skip current post and sort them and pick 5 posts
$posts = $firstCategory->posts()
->where('id', '!=', $id)
->orderBy('views','desc')
->limit(5) // use limit instead of take()
->get();
// use limit instead of take() as we don't need extra data so
// just put limit in sql rather fetching it from db then
// putting limit by code - limit() is more optimised way
return $posts;
});
Good thing now you don't need to add that
category_id
field onrainlab_blog_posts
. So, I guess now you Also don't need to worry about adding it during post insertion.
如有疑问请评论。
如果您只是想显示相关博客 post 而无需自定义博客数据库,可以使用名为 Related Articles 的 OctoberCMS 插件 这需要 rainlab 博客插件。它将显示属于 post 类别的所有其他文章。
- 在您的后端菜单上,转到设置,在系统上,转到更新和插件。
- 转到安装插件。搜索插件相关文章。创建者是 Tallpro。安装它。
- 转到内容管理系统。在您想要相关文章的页面上,转到组件并拖动相关文章。保存并预览。您还可以分叉该组件以编辑您希望相关文章的显示方式。您将在文档中找到更多信息。
如果您遇到任何问题,请随时咨询,或者您可以查看插件评论以了解所面临的问题及其解决方案。