Browse Source

获取图片URL时增加域名

dev
李可松 5 years ago
parent
commit
f7ab79b93d
  1. 3
      app/Models/Advertising.php
  2. 5
      app/Models/Article.php
  3. 10
      app/Models/BaseModel.php
  4. 5
      app/Models/Channel.php
  5. 3
      app/Models/Product.php
  6. 3
      app/Models/Slide.php

3
app/Models/Advertising.php

@ -9,7 +9,6 @@ class Advertising extends BaseModel
use HasFactory; use HasFactory;
public function getPictureAttribute($value) public function getPictureAttribute($value)
{ {
$host = env('APP_URL');
return $host . $value;
return $value ? $this->host . $value : '';
} }
} }

5
app/Models/Article.php

@ -7,4 +7,9 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class Article extends BaseModel class Article extends BaseModel
{ {
use HasFactory; use HasFactory;
public function getImageAttribute($value)
{
return $value ? $this->host . $value : '';
}
} }

10
app/Models/BaseModel.php

@ -6,7 +6,15 @@ use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model class BaseModel extends Model
{ {
protected $dateFormat = 'U'; protected $dateFormat = 'U';
# 不转换时间戳,若使用getUpdatedAtColumn,则不能自动写入时间戳
protected $host = '';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->host = env('APP_URL');
}
# 不转换时间戳,若使用getUpdatedAtColumn,则不能自动写入时间戳
public function getUpdatedAtAttribute($value) public function getUpdatedAtAttribute($value)
{ {
return strtotime($value); return strtotime($value);

5
app/Models/Channel.php

@ -13,4 +13,9 @@ use Illuminate\Database\Eloquent\SoftDeletes;
class Channel extends BaseModel class Channel extends BaseModel
{ {
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
public function getIconAttribute($value)
{
return $value ? $this->host . $value : '';
}
} }

3
app/Models/Product.php

@ -19,6 +19,9 @@ class Product extends BaseModel
public function getPicturesAttribute($value): array public function getPicturesAttribute($value): array
{ {
$pictures = $value ? json_decode($value, true) : []; $pictures = $value ? json_decode($value, true) : [];
foreach ($pictures as &$v) {
$v = $this->host . $v;
}
$this->append('picture'); $this->append('picture');
return $pictures; return $pictures;
} }

3
app/Models/Slide.php

@ -15,7 +15,6 @@ class Slide extends BaseModel
public function getUrlAttribute($value) public function getUrlAttribute($value)
{ {
$host = env('APP_URL');
return $host . $value;
return $value ? $this->host . $value : '';
} }
} }
Loading…
Cancel
Save