<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SitePage extends Model
{
    use HasFactory;
    protected $table = 'site_pages';

    protected $casts = [
        'post_date' => 'datetime:Y-m-d'
    ];



    public function tags()
    {
        return $this->belongsToMany(
            SitePageTag::class,
            'site_pages_tag_relationship',
            'pages_id',
            'tag_id'
        );
    }


    // 根据 ID 获取文章
    public static function getPageById($id)
    {
        return self::where('dist_id', getDistId())->find($id);
    }

    // 根据标题获取文章
    public static function getPageByTitle($title)
    {
        return self::where('dist_id', getDistId())->where('title', $title)->first();
    }

    // 根据 Slug 获取文章
    public static function getPageBySlug($slug)
    {
        return self::where('dist_id', getDistId())->where('slug', $slug)->first();
    }

    // 获取多个文章
    public static function getPages($limit = 10)
    {
        return self::where('dist_id', getDistId())
            ->where('status', 1)
            ->limit($limit)   // 限制结果数量
            ->orderBy('post_date', 'desc')  // 按照发布日期降序排序
            ->get();  // 返回结果集合
    }

}
