File: /home/imensosw/chartapi.imenso.co/app/Http/Controllers/APIController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Chart_data;
use App\Models\Data_record;
use App\Imports\ImportChartData;
use Maatwebsite\Excel\Facades\Excel;
use Validator;
class APIController extends Controller
{
public function upload(Request $request)
{
$file = $request->file('file');
$mimetype = $file->getMimeType();
$allowedFileType = array('application/vnd.ms-excel', 'text/xls', 'text/xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(in_array($mimetype, $allowedFileType)) {
$truncate_sql = Chart_data::query()->truncate();
if (! empty($truncate_sql)) {
$excel = Excel::import(new ImportChartData, request()->file('file'));
if (isset($excel)) {
$fileName = $file->getClientOriginalName();
$dataRecord = new Data_record;
$dataRecord->file_name = $fileName;
$result = $dataRecord->save();
if ($result)
{
$finalOutput = array("type"=>"success", "message"=>"Data imported successfully");
} else {
$finalOutput = array("type"=>"error", "message"=>"Problem in insertion of file details");
}
} else {
$finalOutput = array("type"=>"error", "message"=>"Problem in Importing Excel Data");
}
} else {
$finalOutput = array("type"=>"error", "message"=>"Problem in Deletion of Old Excel Data.");
}
} else {
$finalOutput = array("type"=>"error", "message"=>"Invalid File Type. Upload Excel File.");
}
return json_encode($finalOutput);
}
}