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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…