我有一个与我的Laravel api通信的Ionic应用程序 . 我正在尝试从api获取产品详细信息和图像,并在我的Ionic应用程序中显示该数据 . 我将图像存储在本地磁盘上,并且还有一个包含每个图像的路径名的db表 . 但是当Ionic应用程序获取此数据时,它只会获得一个json对象,其中包含带有本地磁盘路径的mainImage,当然,我不能将其用作img src . 我的问题是,如何将mainImage作为文件而不是json对象传递?
这是我的Laravel功能:
public function api_get_non_disliked_for_user($categoryId)
{
$allProducts = Product::with('mainImage')
->where('subSubCategoryId', "=", $categoryId)->orderBy('title')->get();
return $allProducts;
}
在产品型号中:
public function mainImage(){
return $this->hasOne('App\MainImage', 'productId');
}
在MainImage模型中:
public function product(){
return $this->belongsTo('App\Product', 'productId');
}
1 回答
API应该提供图像的URL,而不是图像文件,特别是当使用Ionic作为前端时,因为那样你只需在你的html标记中使用url,图像就会自动下载 .