Laravel:将上传的文件附加到模型

时间:2018-08-09 13:59:09

标签: php laravel

在我的应用程序中,我有一个文件上传功能,其中所述表的架构如下所示:

Schema::create('file_meta_data', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('category')->nullable();
    $table->string('type');
    $table->string('extension');
    $table->string('size');
    $table->string('filepath');
    $table->timestamps();
});

我还有一个文章模式:

Schema::create('articles', function (Blueprint $table) {
    $table->increments('id', 10);
    $table->string('title');
    $table->string('excerpt')->nullable();
    $table->mediumText('content')->nullable();
    $table->string('featuredImage')->nullable();
    $table->mediumText('featuredVideo')->nullable();
    $table->string('author')->nullable();
    $table->string('readingTime')->nullable();
    $table->string('category')->nullable();
    $table->boolean('featuredArticle')->default(0);
    $table->boolean('IntranetOnly')->default(0);
    $table->string('published')->default('pending');
    $table->timestamps();
});

这些文件通过此方法上传到TemplateController

/**
 * Process uploading of multiple files
 *
 * @param Request $request
 * @return void
 */
public function bulkUpload(Request $request)
{
    // Get every extension that we allowed
    $all_ext = implode(',', $this->allExtensions());

    $this->validate($request, [
        'files.*' => 'required|file|mimes:' . $all_ext . '|max:50000'
    ]);

    // Initialize a file upload counter
    $fileCount = 0;

    // Get the category and department from the form
    $category = $request->get('category');

    // Ensure that the request contains files
    if ($request->hasfile('files')) {
        // Loop through each file and add it to storage
        foreach ($request->file('files') as $file) {

            // Get the meta data for each file
            $name = $file->getClientOriginalName();
            $extension = strtolower($file->getClientOriginalExtension());
            $type = $this->getType($extension);
            $size = $file->getSize();

            // Upload all the given files to Storage, within a specific directory
            if ($category != '') {
                $path = $file->storeAs('library/' . $category, $name);
                $department = null;
            }

            // Grab the filepath so we can store it in the database
            $file->filepath = $path;

            // Create the database entries for the newly uploaded files
            FileMetaData::firstOrCreate([
                'name' => $name,
                'department' => $department,
                'category' => $category,
                'type' => $type,
                'extension' => $extension,
                'size' => $size,
                'filepath' => $path
            ]);

            $fileCount++;
        }

        return redirect('editable/templates-and-tools')->with('success', $fileCount . ' files have been added');
    }
}

最近,我正在考虑将文件附加到应用程序中的Articles。这样,用户可以在文章中提供参考或来源。

我有点复杂,因为在您创建文章的页面中可以有一个文件上传器,但这意味着我必须将TemplateController的完全相同的逻辑应用于{ {1}}

就数据库表而言,我有一个如下所示的架构:

ArticleController

一种选择就是将上传表单指向TemplateController来处理上传吗?


然后唯一的问题就是将新上传的文件链接到文章。

理论上,如果我可以获取上传的文件数组,则可以使用Schema::create('model_has_files', function (Blueprint $table) { $table->increments('id'); $table->integer('file_id')->unsigned(); $table->integer('fileable_id')->unsigned(); $table->string('fileable_type'); $table->foreign('file_id')->references('id')->on('file_meta_data')->onDelete('cascade'); }); 方法将文件附加到文章上。

0 个答案:

没有答案