refactor: 修改文章列表

This commit is contained in:
2025-01-13 17:48:37 +08:00
parent 7cdfa007c6
commit 8aa9db4b7d
3 changed files with 15 additions and 3 deletions

View File

@@ -21,7 +21,6 @@ class Article
$article = ArticleModel::withoutField([ $article = ArticleModel::withoutField([
'language_id', 'language_id',
'category_id',
'author', 'author',
'source', 'source',
'desc', 'desc',
@@ -33,9 +32,10 @@ class Article
'seo_keywords', 'seo_keywords',
'seo_desc' 'seo_desc'
]) ])
->with('category')
->where(function($query) use($param) { ->where(function($query) use($param) {
if (isset($param['category_id'])) { if (isset($param['category_id'])) {
$query->category($param['category_id']); $query->where('category_id', '=', $param['category_id']);
} }
}) })
->withSearch(['title', 'created_at'], (function() use($param) { ->withSearch(['title', 'created_at'], (function() use($param) {
@@ -54,7 +54,12 @@ class Article
->paginate([ ->paginate([
'page' => $param['page'], 'page' => $param['page'],
'list_rows' => $param['limit'], 'list_rows' => $param['limit'],
]); ])
->hidden([
'category',
'category_id'
])
->bindAttr('category', ['category_name' => 'name']);
return success('获取成功', $article); return success('获取成功', $article);
} }

View File

@@ -16,6 +16,12 @@ class ArticleModel extends ArticleBaseModel
// 软删除标记数据字段 // 软删除标记数据字段
protected $deleteTime = 'deleted_at'; protected $deleteTime = 'deleted_at';
// 关联分类
public function category()
{
return $this->belongsTo(ArticleCategoryModel::class, 'category_id', 'id');
}
// 搜索名称 // 搜索名称
public function searchTitleAttr($query, $value, $data) public function searchTitleAttr($query, $value, $data)
{ {

View File

@@ -44,6 +44,7 @@ class CreateArticle extends Migrator
->addColumn('seo_title', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO标题']) ->addColumn('seo_title', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO标题'])
->addColumn('seo_keywords', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO关键字']) ->addColumn('seo_keywords', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO关键字'])
->addColumn('seo_desc', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO描述']) ->addColumn('seo_desc', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => 'SEO描述'])
->addColumn('enabled', 'boolean', ['null' => false, 'default' => 1, 'comment' => '是否启用:1是,-1否'])
->addColumn('release_time', 'datetime', ['null' => false, 'comment' => '发布时间']) ->addColumn('release_time', 'datetime', ['null' => false, 'comment' => '发布时间'])
->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间']) ->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '更新时间']) ->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '更新时间'])