fix: 文章详情推荐文章

This commit is contained in:
2025-06-24 14:13:55 +08:00
parent 9a988de538
commit 06b5d445f7
3 changed files with 22 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ declare (strict_types = 1);
namespace app\index\controller;
use app\admin\controller\v1\ArticleCategory;
use app\index\model\ArticleCategoryModel;
use app\index\model\ArticleLeaveMessageModel;
use app\index\model\ArticleModel;
@@ -115,7 +116,10 @@ class Article extends Common
View::assign('comments', $comments);
// 获取倒序或发布时间倒序3篇文章做为推荐文章
$recommends = ArticleModel::field([
$category_model = new ArticleCategoryModel;
$parent_id = $category_model->bypk($detail['category_id'])->value('pid');
$categorys = $category_model->child($parent_id)->column('id');
$recommends = ArticleModel::field([
'id',
'title',
'desc',
@@ -123,6 +127,7 @@ class Article extends Common
])
->where('id', '<>', $id)
->language($this->lang_id)
->category($categorys)
->order(['release_time' => 'desc', 'id' => 'desc'])
->limit(3)
->select();

View File

@@ -46,6 +46,17 @@ class ArticleCategoryModel extends ArticleCategoryBaseModel
$query->where('pid', '=', $parent);
}
// 所属子分类范围查询
public function scopeChild($query, $id, $merge_self = false)
{
$query->where(function($q) use($id, $merge_self) {
$q->where('pid', '=', $id);
if ($merge_self) {
$q->whereOr('id', '=', $id);
}
});
}
// 是否显示状态范围查询
public function scopeIsShow($query, bool $is_show)
{

View File

@@ -32,6 +32,11 @@ class ArticleModel extends ArticleBaseModel
// 文章分类范围查询
public function scopeCategory($query, $category)
{
if (is_null($category)) return;
if (is_array($category)) {
$query->where('category_id', 'in', $category);
return;
}
$query->where('category_id', '=', $category);
}