Laravel 如何在OctoberCMS中列出相关博客文章
我正在用OctoberCMS构建一个应用程序,希望在每个帖子页面的底部根据类别列出相关的博客帖子。我发现rainlab_blog_posts表并没有指向blog categories表的外键。为了实现我想要的,我考虑扩展blog_posts表,并使用插件添加category_id作为外键。我在表迁移中定义了外键约束。一切似乎都很好。我的挑战是,如何在每次创建新帖子时在posts表中插入category id。OctoberCMS create post backend有一个选项,作者可以通过从列表中选择为新博客文章分配一个类别。只是不知道如何传递此类别id并将其插入类别id字段中的rainlab_blog_posts表中 这就是我希望在API中实现的目标: routes.phpLaravel 如何在OctoberCMS中列出相关博客文章,laravel,octobercms,Laravel,Octobercms,我正在用OctoberCMS构建一个应用程序,希望在每个帖子页面的底部根据类别列出相关的博客帖子。我发现rainlab_blog_posts表并没有指向blog categories表的外键。为了实现我想要的,我考虑扩展blog_posts表,并使用插件添加category_id作为外键。我在表迁移中定义了外键约束。一切似乎都很好。我的挑战是,如何在每次创建新帖子时在posts表中插入category id。OctoberCMS create post backend有一个选项,作者可以通过从列
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\u Blog\u posts
表中找到Category\u id
,因为所有关系都是通过MM关系表维护的rainlab\u blog\u posts\u categories
因此,我认为您将只为博客选择一个类别
,这样您就可以仅为该类别获取相关博客[根据您的代码和描述进行假设]
为此,我们可以利用关系
因此,您的代码可以如下所示
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;
});
好在现在您不需要在rainlab\u blog\u posts
上添加category\u id
字段所以,我想现在您也不必担心在插入后添加它。
如果有任何疑问,请发表评论。嗯,这里似乎有问题
首先,我可以从Blog Plugin
中看到Blog Category
是MM关系,因此您将不在rainlab\u Blog\u posts
表中找到Category\u id
,因为所有关系都是通过MM关系表维护的rainlab\u blog\u posts\u categories
因此,我认为您将只为博客选择一个类别
,这样您就可以仅为该类别获取相关博客[根据您的代码和描述进行假设]
为此,我们可以利用关系
因此,您的代码可以如下所示
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;
});
好在现在您不需要在rainlab\u blog\u posts
上添加category\u id
字段所以,我想现在您也不必担心在插入后添加它。
如果有任何疑问,请发表评论。如果您只想显示相关的博客文章,而不必自定义博客数据库,那么有一个名为
这需要rainlab博客插件。它将显示属于该帖子类别的所有其他文章
在后端菜单上,转到设置,在系统上,转到更新和插件
去安装插件。搜索插件相关文章。创建者是Tallpro。安装它
转到CMS。在需要相关文章的页面上,转到组件并拖动相关文章。保存并预览它。您还可以使用该组件编辑相关文章的显示方式。您将在文档中找到更多信息。
如果您遇到任何问题,请随时查询,或者您可以查看插件评论,了解所面临的问题及其解决方案
如果您只想显示相关的博客文章,而不必自定义博客数据库,那么有一个名为OctoberCMS的插件
这需要rainlab博客插件。它将显示属于该帖子类别的所有其他文章
在后端菜单上,转到设置,在系统上,转到更新和插件
去安装插件。搜索插件相关文章。创建者是Tallpro。安装它
转到CMS。在需要相关文章的页面上,转到组件并拖动相关文章。保存并预览它。您还可以使用该组件编辑相关文章的显示方式。您将在文档中找到更多信息。
如果您遇到任何问题,请随时查询,或者您可以查看插件评论,了解所面临的问题及其解决方案
感谢哈丁的详细回复。我会试试看,然后再回来给你回复。谢谢哈丁的详细回复。我会试试看,然后回来找你,接受答案。