Api 方法文件returnsMethod\Http\Response::文件不存在
我正在尝试向邮递员发送一个包含API响应的文件Api 方法文件returnsMethod\Http\Response::文件不存在,api,laravel-5,file-upload,postman,laravel-5.6,Api,Laravel 5,File Upload,Postman,Laravel 5.6,我正在尝试向邮递员发送一个包含API响应的文件 return response($company)->file($company->logo, $company->main_photo); 拉韦尔伍普斯返回: Method Illuminate\Http\Response::file does not exist. 我做错了什么?我认为您不需要使用responsehelper方法检索文件 它只需要将文件位置发送到前端,例如,假设您的$company对象形状如
return response($company)->file($company->logo, $company->main_photo);
拉韦尔伍普斯返回:
Method Illuminate\Http\Response::file does not exist.
我做错了什么?我认为您不需要使用
response
helper方法检索文件
它只需要将文件位置发送到前端,例如,假设您的$company
对象形状如下:
{
id: 1234,
name: 'My Company',
logo: 'images/companies/logo/1425.jpg'
}
然后,将上述对象传递给前端就足够了,并在合同中要求前端放入http://example.com/files/
在文件地址的开头,或者您可以定义一个JsonResource
类,并用绝对地址覆盖徽标路径(将基本URL追加到开头)
它可能看起来像:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ComapnyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'logo' => 'https://example.com/file/' . $this->logo,
];
}
}