模型篇:
1.根据数据库部分URL返回完整的URL
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>