To import and export CSV and EXCEL file in laravel Maatwebsite provide a package. Maatwebsite laravel package allows you to easily import and export CSV and EXCEL files from your Eloquent Models. Let’s start with an example:-
How to install
If you want to install the package via composer run below command.
composer require maatwebsite/excel
Now we need to configure app.php file. So open your confige/app.php file and change following code:-
'providers' => [ .... 'Maatwebsite\Excel\ExcelServiceProvider', ], 'aliases' => [ .... 'Excel' => 'Maatwebsite\Excel\Facades\Excel', ],
After set the configuration in your confige/app.php file run following command :
php artisan vendor:publish
Now we have to create a route for generating csv and excel file.
Route::post(‘importcsv’, ‘VrsoftcoderController@importCSV’);
Route::post(‘importexcel’, ‘VrsoftcoderController@importExcel’);
Now create VrsoftcoderController.php file in app/Http/Controllers folders look like.
namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use Input; use App\Post; use DB; use Session; use Excel; class VrsoftcoderController extends Controller { public function importExcelFile(Request $request) { if($request->hasFile('import_file')){ Excel::load($request->file('import_file')->getRealPath(), function ($reader) { foreach ($reader->toArray() as $key => $row) { $data['title'] = $row['title']; $data['description'] = $row['description']; if(!empty($data)) { DB::table('post')->insert($data); } } }); } Session::put('success', 'Youe file successfully import in database'); return back(); } public function importCSVFile(Request $request) { if($request->hasFile('import_file')){ Excel::load($request->file('import_file')->getRealPath(), function ($reader) { foreach ($reader->toArray() as $key => $row) { $data['title'] = $row['title']; $data['description'] = $row['description']; if(!empty($data)) { DB::table('post')->insert($data); } } }); } Session::put('success', 'Youe file successfully import in database.'); return back(); } }

To generate CSV and EXCEL file in laravel fllow the below insructions.
Create a route for generating csv and excel file.
Route::get('generatecsv', 'VrsoftcoderController@generateCSV'); Route::get('generateexcel', 'VrsoftcoderController@generateExcel');
Now create VrsoftcoderController.php file in app/Http/Controllers folders look like.
namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use Input; use App\Post; use DB; use Session; use Excel; class VrsoftcoderController extends Controller { public function generateExcelFile() { $ExcelDataArray = array("name","DOB",'email',"phone"); $type = "xls"; return Excel::create('laravelexcel', function($excel) use ($data) { $excel->sheet('mySheet', function($sheet) use ($data) { $sheet->fromArray($ExcelDataArray); }); })->download($type); } public function generateCSVFile() { $ExcelDataArray = array("name","DOB",'email',"phone"); $type = "csv"; return Excel::create('laravelexcel', function($excel) use ($data) { $excel->sheet('mySheet', function($sheet) use ($data) { $sheet->fromArray($ExcelDataArray); }); })->download($type); } }
We hope this article find helpful for you. You can also check our article How to Choose the Best Domain Name. If you liked this article, then please share it on your social networks.
Error: TypeError: Cannot read property map of undefined at resolveDependencies (C:\xampp\htdocs\reactnative\reactnativerealm\drug\node_modules\metro\src\DeltaBundler\traverseDependencies.js
How to get data in DESC order Realm React Native
How to integrate Binect APIs in php
How to delete table in Laravel?