SiteBanner.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class SiteBanner extends Model
  6. {
  7. use HasFactory;
  8. protected $table = 'site_banner';
  9. // 添加一个静态方法获取显示中的Banner(带position和默认值处理)
  10. public static function getBannerById($id, $position = 1)
  11. {
  12. return self::where('id', $id)
  13. ->where('show', 1) // 只获取 show = 1 的 Banner
  14. ->where('position', $position) // 根据 position 过滤
  15. ->where('dist_id', getDistId()) // 根据 dist_id 过滤
  16. ->first();
  17. }
  18. // 添加一个静态方法获取符合条件的多个Banner
  19. public static function getBanners($position = 1, $limit = 10)
  20. {
  21. return self::where('show', 1) // 只获取 show = 1 的 Banner
  22. ->where('position', $position) // 根据 position 过滤
  23. ->where('dist_id', getDistId()) // 根据 dist_id 过滤
  24. ->limit($limit)
  25. ->get();
  26. }
  27. }