在我的 CustomProduct 模型中,我有多个媒体当我发出 GET 请求时,我添加了产品 ID 和媒体 ID。
当我尝试下面的代码时,这是一个雄辩的集合。但我需要它是一个媒体模型,因为我现在不能调用 ->getPath()。
public function downloadMedia($customProduct, $mediaItemId) {
$product = CustomProduct::find($customProduct);
$mediaCollection = $product->getMedia('notes');
$mediaItem = $mediaCollection->where('id', $mediaItemId);
return response()->download($mediaItem->getPath(), $mediaItem->file_name);
}
预期:我重定向到页面并打开下载的文件模型
实际结果:方法 Illuminate\Database\Eloquent\Collection::getPath 不存在。因为它是一个集合而不是一个媒体模型。
阿晨1998
沧海一幻觉