Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

How to export excel after filter in laravel?

I want to export only filtered data in view blade. I am using Laravel 7 and maatwebsite/excel 3.1 and PHP 7.4.2.

I went through the documentation and applied this:

View

<a href="{!! route('users.export-filter') !!}" class="btn btn-success">
    <i class="la la-download"></i>
    Export Filter
</a>

web.php

Route::get('/users/export-filter', 'AdminUserController@filter')->name('users.export-filter');

UserController.php

public function filter()
{
    return Excel::download(new FilterUserExport, 'filter.xlsx');
}

FilterUserExport.php

<?php

namespace AppExports;

use MaatwebsiteExcelConcernsFromView;
use MaatwebsiteExcelConcernsShouldAutoSize;
use MaatwebsiteExcelConcernsWithEvents;
use MaatwebsiteExcelEventsAfterSheet;
use ModulesUserEntitiesUser;
use IlluminateContractsViewView;

class FilterUserExport implements FromView, ShouldAutoSize, WithEvents
{
    /**
     * @return View
     */
    public function view(): View
    {
        $users = app(User::class)->newQuery();

        if ( request()->has('search') && !empty(request()->get('search')) ) {
            $search = request()->query('search');
            $users->where(function ($query) use($search) {
                $query->where('first_name', 'LIKE', "%{$search}%")
                    ->orWhere('last_name', 'LIKE', "%{$search}%")
                    ->orWhere('email', 'LIKE', "%{$search}%")
                    ->orWhere('mobile', 'LIKE', "%{$search}%");
            });
        }
        return view('users.index', compact('users'));
    }

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }
}

export.blade.php

<table>
    <thead style="background-color: green; color: skyblue; border: 3px solid #ee00ee">
    <tr>
        <th>name</th>
        <th>email</th>
        <th>mobile</th>
        <th>national_id</th>
        <th>full_description</th>
        <th>thumbnai</th>
    </tr>
    </thead>
    <tbody>
    @foreach($users as $user)
        <tr>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
            <td>{{ $user->mobile }}</td>
            <td>{{ $user->national_id }}</td>
            <td>{{ $user->full_description  }}</td>
            <td>{{ $user->thumbnailId  }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

This code has not error but my problem did not solve yet

This code show all users but I want to show with filter.

The export submit button is sending everything to Excel. How do I make it to send only the filtered data. Thanks

question from:https://stackoverflow.com/questions/65640741/how-to-export-excel-after-filter-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your filter isn't working because as you mentioned in the comments, the request()->all() returns empty. So you'll need to move your logic into your controller.

UserController.php

public function filter()
{
    $users = app(User::class)->newQuery();

    if ( request()->has('search') && !empty(request()->get('search')) ) {
        $search = request()->query('search');
        $users->where(function ($query) use($search) {
            $query->where('first_name', 'LIKE', "%{$search}%")
                ->orWhere('last_name', 'LIKE', "%{$search}%")
                ->orWhere('email', 'LIKE', "%{$search}%")
                ->orWhere('mobile', 'LIKE', "%{$search}%");
        });
    }

    return Excel::download(new FilterUserExport($users), 'filter.xlsx');
}

FilterUserExport.php

class FilterUserExport implements FromView, ShouldAutoSize, WithEvents
{
    private $users;

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

    /**
     * @return View
     */
    public function view(): View
    {
        return view('users.index', ['users' => $this->users]);
    }

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }
}

Basically I moved your logic from view() to the controller and passed the filtered $users to your Export class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...