【问题标题】:method file returnsMethod Illuminate\Http\Response::file does not exist方法文件返回方法 Illuminate\Http\Response::file 不存在
【发布时间】:2018-09-04 21:28:16
【问题描述】:

我正在尝试向邮递员发送带有我的 API 响应的文件

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

laravel woops 返回:

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

我做错了什么?

【问题讨论】:

    标签: api laravel-5 file-upload postman laravel-5.6


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我认为你也应该回答为什么会出错。
    猜你喜欢
    • 2019-11-12
    • 2021-01-15
    • 2019-05-27
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多