Skip to content

Instantly share code, notes, and snippets.

@manishnakar
Created May 24, 2017 11:19
Show Gist options
  • Save manishnakar/a0843b87b96d5053b26bc865914ff3a1 to your computer and use it in GitHub Desktop.
Save manishnakar/a0843b87b96d5053b26bc865914ff3a1 to your computer and use it in GitHub Desktop.
Excel Download and upload
use Input;
use App\Item;
use DB;
use Excel;
class MaatwebsiteDemoController extends Controller
{
public function importExport()
{
return view('importExport');
}
public function downloadExcel($type)
{
$data = Item::get()->toArray();
return Excel::create('itsolutionstuff_example', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download($type);
}
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = ['title' => $value->title, 'description' => $value->description];
}
if(!empty($insert)){
DB::table('items')->insert($insert);
dd('Insert Record successfully.');
}
}
}
return back();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment