Created
May 24, 2017 11:19
-
-
Save manishnakar/a0843b87b96d5053b26bc865914ff3a1 to your computer and use it in GitHub Desktop.
Excel Download and upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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