Bläddra i källkod

TagCollection 增加 orderBy

moshaorui 1 vecka sedan
förälder
incheckning
e4119104d2
2 ändrade filer med 30 tillägg och 12 borttagningar
  1. 24 12
      app/Models/SitePageTag.php
  2. 6 0
      app/Services/LiquidTags/LiquidTagCollection.php

+ 24 - 12
app/Models/SitePageTag.php

@@ -10,21 +10,33 @@ class SitePageTag extends Model
     use HasFactory;
     protected $table = 'site_pages_tag';
 
-    public function pages($perPage = 10)
+    public function pages($perPage = 10,$orderBy = null)
     {
-        return $this->belongsToMany(
-            SitePage::class,
-            'site_pages_tag_relationship',
-            'tag_id',
-            'pages_id'
-        )->where('status', 1)
-            ->orderBy('post_date', 'desc')
-            ->orderBy('id', 'desc')
-            ->paginate($perPage); // 支持分页
+        if ($orderBy != null) {
+            $orderBy = explode(',', $orderBy);
+            return $this->belongsToMany(
+                SitePage::class,
+                'site_pages_tag_relationship',
+                'tag_id',
+                'pages_id'
+            )->where('status', 1)
+                ->orderBy($orderBy[0], $orderBy[1])
+                ->paginate($perPage); // 支持分页
+        } else {
+            return $this->belongsToMany(
+                SitePage::class,
+                'site_pages_tag_relationship',
+                'tag_id',
+                'pages_id'
+            )->where('status', 1)
+                ->orderBy('post_date', 'desc')
+                ->orderBy('id', 'desc')
+                ->paginate($perPage); // 支持分页
+        }
     }
 
     // 根据slug获取标签和关联的页面
-    public static function getBySlug($slug, $perPage = 10)
+    public static function getBySlug($slug, $perPage = 10,$orderBy = null)
     {
         $tag = self::where('slug', $slug)->where('dist_id', getDistId())->first();
 
@@ -33,6 +45,6 @@ class SitePageTag extends Model
         }
 
         // 获取与标签关联的页面
-        return $tag->pages($perPage);
+        return $tag->pages($perPage,$orderBy);
     }
 }

+ 6 - 0
app/Services/LiquidTags/LiquidTagCollection.php

@@ -12,6 +12,8 @@ class LiquidTagCollection extends AbstractBlock
     private $limit;
     private $templateFile;
 
+    private $orderBy;
+
     // 构造函数解析传入的参数
     public function __construct($markup, array &$tokens, $file_system = null)
     {
@@ -19,6 +21,7 @@ class LiquidTagCollection extends AbstractBlock
         $this->tagSlug = null;
         $this->limit = 10; // 默认显示 10 篇
         $this->templateFile = null;
+        $this->orderBy = null; // title,asc 默认按照文章标题排序
 
         // 正则表达式解析传入的参数
         $syntax = '/(\w+)=("[^"]*"|\'[^\']*\'|\d+)/';
@@ -38,6 +41,9 @@ class LiquidTagCollection extends AbstractBlock
                     case 'template':
                         $this->templateFile = $value; // 设置模板文件
                         break;
+                    case 'orderBy':
+                        $this->orderBy = $value; // 设置模板文件
+                        break;
                 }
             }
         }