Laravel:不允许序列化“ Illuminate \ Http \ UploadedFile”

时间:2019-05-28 13:53:13

标签: php json database laravel

我正在尝试上载图像,将其存储在磁盘上,并将路径添加到存储在数据库中的JSON文件。 每个用户都有一个包含所有图像的JSON文件,例如:

{"images": ["/vendors/57/horse-11.png", "/vendors/57/horse-11.png"]}

该JSON文件存储在数据库中“图像”列中。

几天来我一直在努力解决这个问题。.我总是遇到以下异常:

Serialization of 'Illuminate\Http\UploadedFile' is not allowed

该错误发生在processImage函数的这段代码中:

$image = Image::make($request->file('image'))
                ->resize(750, null, function ($constraint) {
                    $constraint->aspectRatio();
                })
                ->encode('png');

我将指导您进行操作:

首先,用户上传图片,代码:

https://codeshare.io/jGzQ9

其次,通过routes.php,一个控制器被称为:

public function update(User $user, UserHouse $house, UserHouseRequest $request){
    $path = $this->processImage($request, $user->id, $house->id);
    $jsonstring = $house->images;
    array_push($jsonstring['images'], $path);

    $house->images = $jsonstring;
    $house->save();

    return back();
}

private function processImage($request, $userId, $houseId)
{
    $path = null;
    $number = rand(1, 99);
    if ($request->hasFile('image')) {
        $image = Image::make($request->file('image'))
            ->resize(750, null, function ($constraint) {
                $constraint->aspectRatio();
            })
            ->encode('png');
        $path = "/users/{$userId}/house-{$houseId}-{$number}.png";
        Storage::disk('fileadmin')->put($path, $image->encoded);
    }

    return $path;
}

也有一些JS代码,但是我认为这对于此问题不是必需的。如果可以,我以后再添加。

我正在使用Laravel 5.5

stacktrace:

2019-05-28 13:30:49] local.ERROR: Serialization of 'Illuminate\Http\UploadedFile' is not allowed {"userId":57,"email":"info@ho.com","exception":"[object] (Exception(code: 0): Serialization of 'Illuminate\\Http\\UploadedFile' is not allowed at /Users/sg/ah-website/user/laravel/framework/src/Illuminate/Session/Store.php:128)
[stacktrace]
#0 /Users/sg/website/user/laravel/framework/src/Illuminate/Session/Store.php(128): serialize(Array)
#1 /Users/sg/website/user/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(87): Illuminate\\Session\\Store->save()
#2 /Users/sg/website/user/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(218): Illuminate\\Session\\Middleware\\StartSession->terminate(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Response))
#3 /Users/sg/website/user/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(189): Illuminate\\Foundation\\Http\\Kernel->terminateMiddleware(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Response))
#4 /Users/sg/website/public/index.php(60): Illuminate\\Foundation\\Http\\Kernel->terminate(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\Response))
#5 /Users/sg/website/server.php(21): require_once('/Users/sg...')
#6 {main}
"} 

config / filesystems.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default filesystem disk that should be used
    | by the framework. The "local" disk, as well as a variety of cloud
    | based disks are available to your application. Just store away!
    |
    */

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    /*
    |--------------------------------------------------------------------------
    | Default Cloud Filesystem Disk
    |--------------------------------------------------------------------------
    |
    | Many applications store files both locally and in the cloud. For this
    | reason, you may specify a default "cloud" driver here. This driver
    | will be bound as the Cloud disk implementation in the container.
    |
    */

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been setup for each driver as an example of the required options.
    |
    | Supported Drivers: "local", "ftp", "s3", "rackspace"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],

        'fileadmin' => [
            'driver' => 'local',
            'root' => public_path(),
            'visibility' => 'public',
            'url' => config('app.url'),
        ],
    ],

];

0 个答案:

没有答案