davy_yg's avatar
Level 27

Export

Hello,

I wonder how the export command works in laravel?

I am following this tutorial: https://laravel-excel.maatwebsite.nl/3.0/exports/

and still do not understand how to export this data:

public function listReport(Request $request) {

        $report = ReportModel::leftJoin('tb_m_biller_solusi', function($join){
                $join->on('tb_r_solusiorder.productid', '=', 'tb_m_biller_solusi.Productid');
                $join->on('tb_r_solusiorder.billerid', '=', 'tb_m_biller_solusi.Billerid');            
        })
            ->leftJoin('tb_m_biller', 'tb_r_solusiorder.billerid', '=', 'tb_m_biller.biller_id')        
            ->get();

    
            return view('pages.report.list')
                ->with('list', $report);
    
    }

This is the button:

report.list.blade.php

<div class="card mb-3">
        <div class="card-header">
        <div style="float:left">
            <i class="fa fa-table"></i> Reports
        </div>
        <div style="float:right">
                <a class="btn btn-primary" href="{{ url('/create-report') }}" id="toggleNavColor">
                Export Report
            </a>
        </div>
    </div>
0 likes
6 replies
D9705996's avatar

From the tutorial you need to create an export class

php artisan make:export ReportExport --model=ReportModel

In the collectionmethod of ReportExport class you return your eloquent query

public function collection() {
  return ReportModel::leftJoin('tb_m_biller_solusi', function($join){
                $join->on('tb_r_solusiorder.productid', '=', 'tb_m_biller_solusi.Productid');
                $join->on('tb_r_solusiorder.billerid', '=', 'tb_m_biller_solusi.Billerid');            
        })
            ->leftJoin('tb_m_biller', 'tb_r_solusiorder.billerid', '=', 'tb_m_biller.biller_id')        
            ->get();
}

The within your controller you would

use App\Exports\ReportExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;

class ReportController extends Controller 
{
    public function export() 
    {
        return Excel::download(new ReportExport, 'report.xlsx');
    }
}

You will need a route to call the export method

Route::get('/create-report', 'ReportController@export');

If access this route in your browser it should download your report.

davy_yg's avatar
Level 27

I get this command:

C:\xampp\htdocs\ids-api>php artisan make:export ReportExport --model=ReportModel

Command "make:export" is not defined.
Cronix's avatar

@davy_yg That would indicate you haven't installed the excel package, or haven't registered it correctly. Did you install it via composer?

burlresearch's avatar

It may depend on your version of Laravel that you are running: php artisan -V

If it's not recent enough, then you have to add to the providers list in your:

config/app.php

    'providers' => [
    // ...
        /*
         * Package Service Providers...
         */
        Maatwebsite\Excel\ExcelServiceProvider::class,

    // ...
    ],

and if that still doesn't work, then you could try:

$ composer dump-autoload

that should do it, you should be able to verify that that make:export command is in the list:

$ php aritsan list make

This basically is the config described in the setup. Trace it, what's missing?

davy_yg's avatar
Level 27

What should be in : App\ReportExport ?

Class 'App\ReportExport' not found

This is my ReportController which shows the data that I show in the table:

ReportController.php

class ReportController extends Controller
{

    // excel 
 private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }

    public function export()
    {
        return $this->excel->download(new ReportExport, 'report.xlxs');
    }

    // excel ends   

    public function listReport(Request $request) {

        $report = ReportModel::leftJoin('tb_m_biller_solusi', function($join){
                $join->on('tb_r_solusiorder.productid', '=', 'tb_m_biller_solusi.Productid');
                $join->on('tb_r_solusiorder.billerid', '=', 'tb_m_biller_solusi.Billerid');            
        })
            ->leftJoin('tb_m_biller', 'tb_r_solusiorder.billerid', '=', 'tb_m_biller.biller_id')        
            ->get();

    
        return view('pages.report.list')
                ->with('list', $report);
    
    }     

}

When I press Export Report it shows the excel file tables.

report/list.blade.php

<div class="card-header">
          <div style="float:left">
            <i class="fa fa-table"></i> Reports
        </div>
        <div style="float:right">
            <a class="btn btn-primary" href="{{ url('/create-report') }}" id="toggleNavColor">
                Export Report
            </a>
        </div>
    </div>
D9705996's avatar

If you look at my original post ReportExport import is use App\Exports\ReportExport;

Not in App\ReportExport

Please or to participate in this conversation.