| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Game;
- use Illuminate\Database\Eloquent\Model;
- class Banner extends Model
- {
- protected $connection='mysql';
- // 指定表名,如果表名与类名的复数相同则不需要
- protected $table = 'webgame.banners';
- // 确定哪些字段可以被赋值
- protected $fillable = ['img','img_pt','img_es', 'alt', 'link_game', 'link_module'];
- // 隐藏不需要返回的字段
- protected $hidden = ['img_es', 'img_pt'];
- // 关闭自动维护的时间戳,如果您没有在表中创建时间戳字段则需要
- public $timestamps = false;
- public function game()
- {
- return $this->belongsTo(GameCard::class, 'link_game', 'id');
- }
- public function module()
- {
- return $this->belongsTo(PageModule::class, 'link_module', 'id');
- }
- // img 字段的访问器
- public function getImgAttribute($value)
- {
- //处理多语言
- $language=GlobalUserInfo::getLocale();
- switch ($language) {
- case 'es':
- $value= $this->attributes['img_es'] ?? $this->attributes['img_pt'] ?? $value;
- break;
- case 'pt':
- $value= $this->attributes['img_pt'] ?? $this->attributes['img_es'] ?? $value;
- break;
- }
- $cdn_org=["cdn.moeda777.com"];
- $cdn_replace="24680.imgix.net";
- $img_add_param="?auto=format,compress&cs=srgb&dpr=2&w=500";
- $img_add_param="";
- $value=str_replace($cdn_org,$cdn_replace,$value).$img_add_param;
- $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
- if (strstr($origin, "cereja")) {
- $value=str_replace("banner/","banner_cereja/",$value);
- }
- return $value;
- }
- // 自定义序列化的数组形式
- public function toArray()
- {
- $array = parent::toArray();
- // 添加动态生成的 img 字段
- $array['img'] = $this->img;
- // 移除 img_es 和 img_pt 字段
- unset($array['img_es'], $array['img_pt']);
- return $array;
- }
- }
|