1

I am trying to upload a CSV file with one row using Laravel. Its content is to be stored in the already made database (MySQL) which I have, however I am having great difficulty in doing so.

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:

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?

3
  • You have a file upload, you should read up on laravel.com/docs/5.6/filesystem#file-uploads Commented Mar 10, 2018 at 0:24
  • first of all your form must have "enctype=multipart/form-data" attribute to let you to upload file Commented Mar 10, 2018 at 0:52
  • First, you need to store it on your server. Then, you can use php fgetcsv to read the file. I'll make an answer explaining in detail whenever I can if you still need Commented Mar 10, 2018 at 1:38

1 Answer 1

1

Add the below code in your HTML form tag

<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

E.g

    DB_CONNECTION=mysql

    DB_HOST=127.0.0.1

    DB_PORT=3306

    DB_DATABASE=your_database_name

    DB_USERNAME=root

    DB_PASSWORD=
Sign up to request clarification or add additional context in comments.

3 Comments

Hello thank you for your answer, i am getting a 'Excel' class not found' error on this line : $data = \Excel::load($path)->get();, do i need to improt an excel class?
You need to use Excel before your class begins kindly refer : googleweblight.com/i?u=https://laravelcode.com/post/…
I hope this worked for you as you expected if yes please up vote the answer so that others can reach it easier thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.