SitePage.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class SitePage extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'site_pages';
  9. protected $casts = [
  10. 'post_date' => 'datetime:Y-m-d'
  11. ];
  12. public function tags()
  13. {
  14. return $this->belongsToMany(
  15. SitePageTag::class,
  16. 'site_pages_tag_relationship',
  17. 'pages_id',
  18. 'tag_id'
  19. );
  20. }
  21. // 根据 ID 获取文章
  22. public static function getPageById($id)
  23. {
  24. return self::where('dist_id', getDistId())->find($id);
  25. }
  26. // 根据标题获取文章
  27. public static function getPageByTitle($title)
  28. {
  29. return self::where('dist_id', getDistId())->where('title', $title)->first();
  30. }
  31. // 根据 Slug 获取文章
  32. public static function getPageBySlug($slug)
  33. {
  34. return self::where('dist_id', getDistId())->where('slug', $slug)->first();
  35. }
  36. // 获取多个文章
  37. public static function getPages($limit = 10)
  38. {
  39. return self::where('dist_id', getDistId())
  40. ->where('status', 1)
  41. ->limit($limit) // 限制结果数量
  42. ->orderBy('post_date', 'desc') // 按照发布日期降序排序
  43. ->get(); // 返回结果集合
  44. }
  45. }