If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
इस topic में आप सीखेंगे कि कैसे Laravel 8 में blade template का use करके excel file को export करते हैं। बैसे तो आप normal PHP code से भी excel / csv file generate कर सकते हैं , लेकिन उसमे आप image insert नहीं कर पायंगे।
I Hope, आपने Laravel project install कर लिया होगा , अगर नहीं किया तो नीचे दी गयी command run करके project install कर लें।
composer create-project --prefer-dist laravel/laravel projectName
Laravel 8 में excel file export करने के लिए maatwebsite/excel package का use करेंगे , तो सबसे पहले package install करते हैं।
composer require maatwebsite/excel
Next , अब हमें providers और alias add करने होंगे जिससे हम , short name के साथ package को access कर सकें। इसके लिए config/app.php file open करने और providers और alias Array में नीचे दी गयी lines add कर दें।
File : config/app.php 'providers' => [ .... Maatwebsite\Excel\ExcelServiceProvider::class, ], 'aliases' => [ .... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ],
File : routes/web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ExportController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('export', [ExportController::class, 'export'])->name('export');
अब हमें , एक Export class बनानी है , जिसमे हम पर वो data return करेंगे , जो हमें export करना है।
php artisan make:export DataExport
ये command run करने पर app/Exports/DataExport.php file create होगी , इसे open करें और जो भी data आपको import करना है , वो return कर दें।
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class DataExport implements FromCollection
{
/**
* @description return data that you want to export.
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return collect([
[
'name' => 'User1',
'age' => 25,
'address' => 'User Addres 1'
],
[
'name' => 'User2',
'age' => 27,
'address' => 'User Addres 2'
],
]);
}
}
Note : हमने यह dummy data Array return किया है हालाँकि आप चाहें तो , आप किसी Model का use करके Database से भी data return कर सकते हैं।
Next , अब हमें एक ExportController create करना है जिसे routes/web.php file में use किया था।
<?php
namespace App\Http\Controllers;
use Excel;
use App\Exports\DataExport;
class ExportController extends Controller
{
/**
* Function to export data in excel file
* @return \Illuminate\Support\Collection
*/
public function export()
{
return Excel::download(new DataExport, 'excle-test.xlsx');
}
php artisan serve
hit 127.0.0.1:8000/export URL
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook