Banner.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Game;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Banner extends Model
  5. {
  6. protected $connection='mysql';
  7. // 指定表名,如果表名与类名的复数相同则不需要
  8. protected $table = 'webgame.banners';
  9. // 确定哪些字段可以被赋值
  10. protected $fillable = ['img','img_pt','img_es', 'alt', 'link_game', 'link_module'];
  11. // 隐藏不需要返回的字段
  12. protected $hidden = ['img_es', 'img_pt'];
  13. // 关闭自动维护的时间戳,如果您没有在表中创建时间戳字段则需要
  14. public $timestamps = false;
  15. public function game()
  16. {
  17. return $this->belongsTo(GameCard::class, 'link_game', 'id');
  18. }
  19. public function module()
  20. {
  21. return $this->belongsTo(PageModule::class, 'link_module', 'id');
  22. }
  23. // img 字段的访问器
  24. public function getImgAttribute($value)
  25. {
  26. //处理多语言
  27. $language=GlobalUserInfo::getLocale();
  28. switch ($language) {
  29. case 'es':
  30. $value= $this->attributes['img_es'] ?? $this->attributes['img_pt'] ?? $value;
  31. break;
  32. case 'pt':
  33. $value= $this->attributes['img_pt'] ?? $this->attributes['img_es'] ?? $value;
  34. break;
  35. }
  36. $cdn_org=["cdn.moeda777.com"];
  37. $cdn_replace="24680.imgix.net";
  38. $img_add_param="?auto=format,compress&cs=srgb&dpr=2&w=500";
  39. $img_add_param="";
  40. $value=str_replace($cdn_org,$cdn_replace,$value).$img_add_param;
  41. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  42. if (strstr($origin, "cereja")) {
  43. $value=str_replace("banner/","banner_cereja/",$value);
  44. }
  45. return $value;
  46. }
  47. // 自定义序列化的数组形式
  48. public function toArray()
  49. {
  50. $array = parent::toArray();
  51. // 添加动态生成的 img 字段
  52. $array['img'] = $this->img;
  53. // 移除 img_es 和 img_pt 字段
  54. unset($array['img_es'], $array['img_pt']);
  55. return $array;
  56. }
  57. }