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?