<?php

namespace App\Models;

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

class SitePageTag extends Model
{
    use HasFactory;
    protected $table = 'site_pages_tag';

    public function pages($perPage = 10)
    {
        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)
    {
        $tag = self::where('slug', $slug)->first();

        if (!$tag) {
            return null; // 没找到标签
        }

        // 获取与标签关联的页面
        return $tag->pages($perPage);
    }
}
