Reputation: 506
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', 'Admin\UserController@filter')->name('users.export-filter');
UserController.php
public function filter()
{
return Excel::download(new FilterUserExport, 'filter.xlsx');
}
FilterUserExport.php
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Modules\User\Entities\User;
use Illuminate\Contracts\View\View;
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
Upvotes: 2
Views: 7896
Reputation: 2158
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.
Upvotes: 2