方法文件返回方法Illuminate\Http\Response::文件不存在

method file returnsMethod Illuminate\Http\Response::file does not exist

我正在尝试发送一个文件,其中包含我对邮递员 API 的回复

        return response($company)->file($company->logo, $company->main_photo);

laravel 呜呜呜 returns:

Method Illuminate\Http\Response::file does not exist.

我做错了什么?

我认为您不需要使用 response 辅助方法检索文件。

只需要将文件位置发送给前端即可,例如假设您的 $company 对象形状类似于:

{
    id: 1234,
    name: 'My Company',
    logo: 'images/companies/logo/1425.jpg'
}

然后将上述对象传递给您的前端就足够了,并且在合同中要求您的前端将 http://example.com/files/ 放在文件地址的开头,或者您可以定义一个 JsonResource class 并用绝对地址覆盖徽标路径(将 base-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,
        ];
    }
}

看看 documentation.