Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite

Today, We want to share with you Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite example.In this post we will show you import excel file in laravel, hear for import and export csv file in laravel we will give you demo and example for implement.In this post, we will learn about laravel 5.6 import csv to database with an example.

Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite example

There are the Following The simple About laravel csv import package Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel maatwebsiteexcel export, so the laravel excel 3.1 export example is used for this example is following below.

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

Read Also:  Laravel 9 Resize Image Before Upload Example

Keywords : Laravel 5 import export to excel and csv using maatwebsite example, laravel maatwebsiteexcel export, laravel excel 3.1 import example, laravel excel export example, laravel excel 3.1 export example, laravel csv import package, import and export csv file in laravel, laravel 5.6 import csv to database, import excel file in laravel

Phase 1: Installation

composer.json

//Laravel 5
"maatwebsite/excel": "~2.1.0"

//Laravel 4
"maatwebsite/excel": "~1.3"

Then, run command composer update

config/app.php

'providers' => [
	....

	'Maatwebsite\Excel\ExcelServiceProvider',

],

'aliases' => [

	....

	'Excel' => 'Maatwebsite\Excel\Facades\Excel',

],

Config

//Laravel 5 then fire following command:
php artisan vendor:publish

//Laravel 4 then fire following command:
php artisan config:publish maatwebsite/excel

Phase 3: Make a Table and Model

php artisan make:migration create_tasks_table

database/migrations

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
   public function up()
   {
       Schema::create('tasks', function (Blueprint $table) {
           $table->increments('id');
           $table->string('title');
           $table->text('description');
           $table->timestamps();
       });
   }
   public function down()
   {
       Schema::drop("tasks");
   }
}

app/Task.php

namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
   public $fillable = ['title','description'];
}

Phase 3: Make a Route

app/Http/routes.php

Route::get('importExport', '[email protected]');
Route::get('forceExcelFileLoad/{type}', '[email protected]');
Route::post('excelDataImp', '[email protected]');

Phase 4: Make a Controller

app/Http/Controllers/MaatwebsiteBdayWishController.php

use Input;
use App\Task;
use DB;
use Excel;
class MaatwebsiteBdayWishController extends Controller
{
	public function importExport()
	{
		return view('importExport');
	}
	public function forceExcelFileLoad($type)
	{
		$data = Task::get()->toArray();
		return Excel::create('pakainfo_example', function($excel) use ($data) {
			$excel->sheet('mySheet', function($sheet) use ($data)
	        {
				$sheet->fromArray($data);
	        });
		})->download($type);
	}
	public function excelDataImp()
	{
		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('tasks')->insert($insert);
					dd('Insert Record successfully.');
				}
			}
		}
		return back();
	}
}

Phase 5: Make a View

importExport.blade.php

<html lang="en">
<head>
	<title>Import - Export Laravel 5</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<div class="navbar-header">
				<a class="navbar-brand" href="#">Import - Export in Excel and CSV Laravel 5</a>
			</div>
		</div>
	</nav>
	<div class="container">
		<a href="{{ URL::to('forceExcelFileLoad/xls') }}"><button class="btn btn-success">Download Excel xls</button></a>
		<a href="{{ URL::to('forceExcelFileLoad/xlsx') }}"><button class="btn btn-success">Download Excel xlsx</button></a>
		<a href="{{ URL::to('forceExcelFileLoad/csv') }}"><button class="btn btn-success">Download CSV</button></a>
		<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('excelDataImp') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
			<input type="file" name="import_file" />
			<button class="btn btn-success">Import File</button>
		</form>
	</div>
</body>
</html>

Web Programming Tutorials Example with Demo

Read :

Read Also:  laravel checking if record exists

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel excel 3.1 import example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.