konrms
7 months ago
836
13
Laravel

How to export database table to excel file in laravel

Posted 7 months ago by konrms

Hi guys.

I'm trying to export the table "formal", to a simple excel file. I have installed the packet necessary with "composer require maatwebsite/excel" command from cmd and also have edited my app.php file adding "Maatwebsite\Excel\ExcelServiceProvider::class," to providers and "'Excel' => Maatwebsite\Excel\Facades\Excel::class," to aliases.

My blade successfully shows "formal" as html. Now I have added a second button with export to excel option. Unfortunately, I'm getting an error "Call to undefined method Maatwebsite\Excel\Excel::create()" when pressing "Download excel button".

Could you help me please?

This is my blade with the "Download excel" button added.

<!DOCTYPE html>
<html lang="el">
<head>
    <meta charset="UTF-8">
    <title>Πρόγραμμα Σπουδών</title>
    
</head>
<body>

<b>Πρόγραμμα Σπουδών: {{ $key }}</b>
<br>
<br>

<table border="1">
    <tr>
        <th>Κατηγορία μαθήματος</th>
        <th>Κωδικός μαθήματος</th>
        <th>Τίτλος μαθήματος</th>
        <th>Τμήμα μαθήματος</th>
        <th>ΠΜΣ</th>
        <th>Εργαστηριακή Μονάδα</th>
        <th>Τμήμα Εργαστηριακής μονάδας</th>
        <th>Username Upnet Διδάσκοντος</th>
        <th>Επίθετο Διδάσκοντος</th>
        <th>Όνομα Διδάσκοντος</th>
        <th>Τμήμα Διδάσκοντος</th>
        <th>Βαθμίδα Διδάσκοντος</th>
        <th>Ακαδημαϊκό εξάμηνο</th>
        <th>Υποχρεωτικό Μάθημα</th>
        <th>Συνδιδασκαλία</th>
        <th>Ακροατήριο</th>
    </tr>
    @foreach($star as $v2)
    <tr>
        <th>{{ $v2->lesson_category }}</th>
        <td>{{ $v2->lesson_code }}</td>
        <td>{{ $v2->lesson_title }}</td>
        <td>{{ $v2->lesson_dep }}</td>
        <td>{{ $v2->pms }}</td>
        <td>{{ $v2->ergastitiaki_monada }}</td>
        <td>{{ $v2->tmima_ergastitiakis_monadas }}</td>
        <td>{{ $v2->username }}</td>
        <td>{{ $v2->epitheto }}</td>
        <td>{{ $v2->onoma }}</td>
        <td>{{ $v2->tutor_dep }}</td>
        <td>{{ $v2->vathmida }}</td>
        <td>{{ $v2->semester }}</td>
        <td>{{ $v2->ypoxreotiko }}</td>
        <td>{{ $v2->syndidaskalia }}</td>
        <td>{{ $v2->akroatirio }}</td>
    </tr>
    @endforeach
</table>
<br>


<form action = '/excel'>
    <input type="submit" value="Download excel">
<form>


<form action ='/programma'>
    <input type="submit" value="Νέα Αναζήτηση" />
</form>


</body>
</html>

This is the ExportExcelController:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;  
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;  

class ExportExcelController extends Controller
{
    public function excel()
    {
        $formal_data = DB::table('formal')->get()->toArray();
        $programma_array[] = array('lesson_category', 'lesson_code', 'lesson_title', 'lesson_dep', 'pms',
        'ergastitiaki_monada', 'tmima_ergastitiakis_monadas', 'username', 'epitheto', 'onoma', 'tutor_dep', 'vathmida', 
        'semester', 'ypoxreotiko', 'syndidaskalia', 'akroatirio');
            
        foreach($formal_data as $pass)
        {
            $programma_array[] = array
            (
            'lesson_category'  => $pass->lesson_category,
            'lesson_code'   => $pass->lesson_code,
            'lesson_title'    => $pass->lesson_title,
            'lesson_dep'  => $pass->lesson_dep,
            'pms'   => $pass->pms,
            'ergastitiaki_monada'   => $pass->ergastitiaki_monada,
            'tmima_ergastitiakis_monadas'   => $pass->tmima_ergastitiakis_monadas,
            'username'   => $pass->username,
            'epitheto'   => $pass->epitheto,
            'onoma'   => $pass->onoma,
            'tutor_dep'   => $pass->tutor_dep,
            'vathmida'   => $pass->vathmida,
            'semester'   => $pass->semester,
            'ypoxreotiko'   => $pass->ypoxreotiko,
            'syndidaskalia'   => $pass->syndidaskalia,
            'akroatirio'   => $pass->akroatirio,                
            );
            
        }
        
            
            Excel::create('Programma_Data', function($excel) use ($programma_array)
            {
                $excel->setTitle('Apotelesmata');
                $excel->sheet('Programma_Data', function($sheet) use ($programma_array)
                {
                    $sheet->fromArray($programma_array, null, 'A1', false, false);
                });
            }) -> download('xlsx');
            
            
    }
               
}       

And my web.php

Route::get('/excel', '[email protected]')->name('export_excel.excel');

Please sign in or create an account to participate in this conversation.