兵兵有你

人品好,气质差.丢了工作就回家...

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  在模型product.php中增加以下方法.数据库图片字段为image.存储的图片相对路径 

 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
因此在模板中图片路径就得写$product->image_url

  

posted on 2019-08-22 14:01  greatbing  阅读(2989)  评论(0编辑  收藏  举报