简体   繁体   English

当我使用Laravel将文件上传到MySQL时,我的数据库表中有两个分隔的行用于每个请求

[英]When I upload file to MySQL using Laravel, there are two separated lines in my database table for each request

I'm working on a Laravel web application that should upload .pdf files from a form to database, with some specific information inserted by the user in the same form where he uploads the file. 我正在开发一个Laravel Web应用程序,它应该将.pdf文件从表单上传到数据库,用户在上传文件的同一表单中插入一些特定信息。 Everything works perfectly, but in the table on my database, I have two separated rows created. 一切都很完美,但在我的数据库表中,我创建了两个独立的行。 First one contains just file_name (Original Client name) and file_size (Original Client size), but other form information set to their default values (NULL), in the second row I have the opposite, additional form data that is set correctly, but the file_name and file_size are set to default (NULL) here is my function in controller block 第一个包含file_name(原始客户端名称)和file_size(原始客户端大小),但其他表单信息设置为其默认值(NULL),在第二行中我有相反的,正确设置的其他表单数据,但是file_name和file_size设置为default(NULL)这是我在控制器块中的函数

public function auteurAdd(Request $request)
{
    $parameters = $request->except(['_token']);
    $conference = new Conference();
    $conference->titre = $parameters['titre'];
    $conference->theme = $parameters['theme'];
    $conference->track = $parameters['track'];

    // upload file into database
    if ($request->hasFile('file')) {
        $filename = $request->file->getClientOriginalName();
        $filesize = $request->file->getClientSize();
        $file = new Conference;
        $file->file_name = $filename;
        $file->file_size = $filesize;
        $file->save();
    }
    $conference->save();

    return redirect()->route('auteurHome');
}

and this is the form 这是表格

<form method="post" action="{{route('auteurAdd')}}" class="text-center border border-light p-5"
      enctype="multipart/form-data">
    {{ csrf_field() }}
    <p class="h4 mb-4">Upload d'une conférence</p>
    <div class="form-group">
        <input name="titre" class="form-control mb-4" placeholder="Titre"/>
        <input name="theme" class="form-control mb-4" placeholder="Thème"/>
        <input name="track" class="form-control mb-4" placeholder="Track"/>
    </div>
    <div class="btn btn-mdb-color btn-rounded float-left">
        <input type="file" name="file">
    </div>
    <button class="btn btn-info btn-block my-4" type="submit">Envoyer</button>
</form>

Try like this: 试试这样:

public function auteurAdd(Request $request)
{
    $parameters = $request->except(['_token']);
    $conference = new Conference();
    $conference->titre = $parameters['titre'];
    $conference->theme = $parameters['theme'];
    $conference->track = $parameters['track'];

    // upload file into database
    if ($request->hasFile('file')) {
        $filename = $request->file->getClientOriginalName();
        $filesize = $request->file->getClientSize();
        $conference->file_name = $filename;
        $conference->file_size = $filesize;
    }
    $conference->save();

    return redirect()->route('auteurHome');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM