Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

saadaan's avatar

Getting excel export from blade using laravel-excel

Hi,

I am using laravel-excel package for excel export, and successfully downloading the data from controller method as given in the docs. However, I have some customization in the blade page. Is there a method to download the data displayed in the TABLE in the blade view directly from the page instead of using controller method?

Thanks, Saad

0 likes
4 replies
adiputra22's avatar

hi Saad,

If without controller, you need use javascript to handle that. coz i think laravel-excel is under server (PHP).

You can search in google how to do that with javascript. Hopely can help you.

MarianoMoreyra's avatar

Hi @saadaan

If you are talking about the Maatwebsite package, they have an option to export from a Blade view instead of Collection.

https://docs.laravel-excel.com/3.1/exports/from-view.html

Although, if the customization you mention is mostly cosmetic, I'm not sure if that option will meet your needs.

If you need to do some styling, it can be done exporting from Collections or Model if I'm not wrong. Take a look at the styling section at "Customizing Columns" in that case to see if that works for you:

https://docs.laravel-excel.com/3.1/exports/column-formatting.html#styling

Hope this helps!

saadaan's avatar

Hi @marianomoreyra,

Thanks for your reply. The first linked you pasted, I am not really sure if it is getting data from the view. The download link is referring to the controller method which is calling the class instance directly.

My problem is that in the HTML table, I am indexing the data of model 1 (which I am getting from the controller and passing onto the view) into model 2 to get some additional data, and putting that data into the final HTML table. This might be done at controller level, but will require a lot of programming. Since I am already doing it at blade table, I want the download to get triggered from the blade view. Is it possible?

Thanks, Saad

MarianoMoreyra's avatar

Well @saadaan

I've gone to the link again, and I see the docs for exporting from a View...although, as with every view, it depends on a Controller to get the information to show.

So, I guess you should replicate what the current view controller does, in the following method as suggested at docs:

namespace App\Exports;

use App\Invoice;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class InvoicesExport implements FromView
{
    public function view(): View
    {
	// Here you should do the same you do at your 
        // other controller to pass data to the view

	// and here you'll return that corresponding view
        return view('exports.invoices', [
            'invoices' => Invoice::all()
        ]);
    }
}

Then at the controller that responds to the actual action of requesting to export, you do the following as the docs suggest:

public function export() 
{
    return Excel::download(new InvoicesExport, 'invoices.xlsx');
}

Now, if what you want to do is to export a Datatable AFTER the user have sorted or filtered results at client side, then you'll need a Javascript export library to achieve that.

Hope this makes sense to you

Please or to participate in this conversation.