简体   繁体   English

使用Laravel将CSV内容上传到数据库

[英]Uploading CSV contents to database using Laravel

I am trying to upload a CSV file with one row using Laravel. 我正在尝试使用Laravel上载一行CSV文件。 Its content is to be stored in the already made database (MySQL) which I have, however I am having great difficulty in doing so. 它的内容存储在我已经拥有的数据库(MySQL)中,但是这样做非常困难。

Here is my form: 这是我的表格:

    <center>
    <form action="/csvfileupload" method="post">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <h4><label> Please Select File (CSV only):</label>
    <input type="file" name="csvfile"/></h4>
    <h4><input type="submit" name="upload" value="Upload"/></h4>
    </form>
    {{ csrf_field() }}
    </center>

Here is my route: 这是我的路线:

    Route::post('/csvfileupload','Controller@csvfileupload');

Here is my controller: 这是我的控制器:

function csvfileupload(Request $req)
{
    $title = $req->input('title');
    $address = $req->input('address');
    $Date = $req->input('Date');
    $intro = $req->input('intro');
    $mainbody = $req->input('mainbody');
    $paragraph = $req->input('paragraph');
    $footer = $req->input('footer');

    $data =
    array('title'=>$title,'address'=>$address,'Date'=>$Date,'intro'=>$intro,
   'mainbody'=>$mainbody,'paragraph'=>$paragraph,'footer'=>$footer);
    DB::table('template')->update($data);

    echo "Success";
}

CSV file: CSV档案:

title,address,Date,intro,mainbody,paragraph,footer
Module Bro,"Hi,",2018-03-14,Attention all students!: This is very 
important!,"Your results are out now on blackboard, you have received a 
grade of:",Comments for your coursework are as follows if you have any 
concerns feel free to contact me at any time.,"Yours Truly, best tutor ever. 
helen d."

I want to store them in the database, but how can I do that? 我想将它们存储在数据库中,但是我该怎么做呢?

Add the below code in your HTML form tag 将以下代码添加到HTML表单标签中

<form action="/csvfileupload" method="post" enctype="multipart/form-data">

Controller code: 控制器代码:

public function csvfileupload(Request $req)
{
    if ($request->hasFile('csvfile')) {
        $path = $request->file('csvfile')->getRealPath();
        $data = \Excel::load($path)->get();

        if ($data->count()) {
            foreach ($data as $key => $value) {
                $arr[] = ['title' => $value->title, 
                          'address' => $value->address,
                          'Date' => $value->Date,
                          'intro' => $value->intro,
                          'mainbody' => $value->mainbody,
                          'paragraph' => $value->paragraph,
                          'footer' => $value->footer,

                         ];
            }
            if (!empty($arr)) {
                DB::table('template')->insert($arr);

                return "Success";
            }
        }
    }
}

Note: Make sure you update your .env file with your database settings like 注意:请确保使用数据库设置(例如)更新.env文件

Eg 例如

    DB_CONNECTION=mysql

    DB_HOST=127.0.0.1

    DB_PORT=3306

    DB_DATABASE=your_database_name

    DB_USERNAME=root

    DB_PASSWORD=

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

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