I have Core php code for importing large csv file into mysql Database and it works fine. i want to use this same technique in laravel and want to convert it into laravel's controller function. below i'm attaching my core php code please help me if anybody can.
<?php
session_start();
require_once('Database.php');
if(isset($_SESSION['admin']) and $_SESSION['admin']!="")
{
if (!empty($_FILES['csvfile']['name']))
{
//$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
$mimes = array(
'text/csv',
'text/plain',
'application/csv',
'text/comma-separated-values',
'application/excel',
'application/vnd.ms-excel',
'application/vnd.msexcel',
'text/anytext',
'application/octet-stream',
'application/txt',
);
if(in_array($_FILES['csvfile']['type'],$mimes)){
$file=$_FILES['csvfile']['tmp_name'];
$fname=basename($file);
//$sql="delete from calls";
//mysql_query($sql) or die(mysql_error());
$sql="LOAD DATA LOCAL INFILE '/tmp/$fname'
INTO TABLE calls
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";
mysql_query($sql) or die(mysql_error());
header('location:import-csv.php?success=1');
unlink($file);
} else {
header('Location:import-csv.php?invalid=1');
}
}
else header('Location:import-csv.php?mandatory=1');
}
else header('Location:index.php');
?>
Here i have tried to convert it into a function but its incomplete. i actually don't know how to execute/run this query in that function. this function is in Controller.
public function import(Request $request)
{
//Excel::import(new CallsImport,request()->file('file'));
//return redirect()->route('calls.importExportView')->withFlashSuccess(__('Added.'));
if ($request->hasFile('file')){
$csv = $request->file('file');
$query="LOAD DATA INFILE '$csv'
INTO TABLE calls
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";
}
}