Php Laravel 5.6在数据库中存储文件并保存

Php Laravel 5.6在数据库中存储文件并保存,php,laravel,Php,Laravel,用户可以在创建新ImageRequest时上载1个或多个文件(例如图像)作为附件 我的目标是将附件保存在数据库或lea中,以便知道哪些附件属于哪个ImageRequest。显然我需要保存文件 这简化了ImageRequest表的外观: Schema::create('image_requests', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteg

用户可以在创建新ImageRequest时上载1个或多个文件(例如图像)作为附件

我的目标是将附件保存在数据库或lea中,以便知道哪些附件属于哪个ImageRequest。显然我需要保存文件

这简化了ImageRequest表的外观:

Schema::create('image_requests', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id')->nullable();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('attachment')->nullable();
        $table->timestamps();
});
这是我的文件上传在我的表格

<input
        id="input-b3"
        name="attachment[]"
        type="file"
        class="file"
        multiple
        data-show-upload="false"
        data-show-caption="true"
        data-msg-placeholder="Select {files} for upload..."
>
我得到以下错误:

数组到字符串的转换

这可能是因为我的列类型是string,我正在尝试保存一个数组,所以我需要序列化它,但我不知道我所做的是如何工作的,或者是否工作正常


从未想过在Laraven中存储附件会如此困难,特别是因为Laravel使web应用程序的基本内容变得相当简单。

对于我的项目,我就是这么做的,并且是在您的构建中工作的,请将此

   $this->upload_path = 'img'.DIRECTORY_SEPARATOR.'blog'.DIRECTORY_SEPARATOR;
    $this->storage = Storage::disk('public');
之后,在函数中使用以下命令:

 // Uploading Image
    if (array_key_exists('featured_image', $input)) {
        $this->deleteOldFile($blog);
        $input = $this->uploadImage($input);
    }
这是deleteOldFile

 public function deleteOldFile($model)
{
    $fileName = $model->featured_image;

    return $this->storage->delete($this->upload_path.$fileName);
}
并上传图像

公共函数上传图像($input) { $avatar=$input['featured_image']

if (isset($input['featured_image']) && !empty($input['featured_image'])) {
    $fileName = time().$avatar->getClientOriginalName();

    $this->storage->put($this->upload_path.$fileName, file_get_contents($avatar->getRealPath()));

    $input = array_merge($input, ['featured_image' => $fileName]);

    return $input;
}

}

此错误发生在哪一行?您可以通过从数据库获取文件的路径来访问它,而不是存储实际文件,以便在数据库中显示文件的路径。此外,如果设置了存储文件,则需要更改
$table->string('attachment')->nullable()到一个blob,而不是一个字符串。是的,这就是我试图存储的路径@Script47@Script47但问题是,可能有多个文件,因此我的sql中的列需要接受和数组或序列化数组。我不知道如何使用我的当前代码执行此操作,然后只需
json\u encode
文件路径数组,在检索时,
json\u decode
(第二个参数为true)并在数组中循环以获取路径。另一种选择是
图像\u请求
附件
->一个
图像\u请求
具有多个附件之间的1:m关系。创建一个
attachments
表,并在
image\u请求
和此请求之间添加关系。从
image\u请求
表中删除附件列
if (isset($input['featured_image']) && !empty($input['featured_image'])) {
    $fileName = time().$avatar->getClientOriginalName();

    $this->storage->put($this->upload_path.$fileName, file_get_contents($avatar->getRealPath()));

    $input = array_merge($input, ['featured_image' => $fileName]);

    return $input;
}