模型篇:

1.根据数据库部分URL返回完整的URL

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
    public function getImageUrlAttribute()
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($this->attributes['image'], ['http://', 'https://'])) {
            return $this->attributes['image'];
        }
        return \Storage::disk('public')->url($this->attributes['image']);
    }
//模板中Laravel 的模型访问器会自动把下划线改为驼峰,所以 image_url 对应的就是 getImageUrlAttribute
<div class="img"><img src="{{ $product->image_url }}" alt=""></div>

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄