【发布时间】:2021-01-09 08:31:21
【问题描述】:
我只想导出视图刀片中的过滤数据。我正在使用 Laravel 7 和 maatwebsite/excel 3.1 和 PHP 7.4.2。
我浏览了文档并应用了这个:
查看
<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');
用户控制器.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>
这段代码没有错误,但我的问题还没有解决
此代码显示所有用户,但我想用过滤器显示。
导出提交按钮将所有内容发送到 Excel。如何使其仅发送过滤后的数据。谢谢
【问题讨论】:
-
有人可以回答我的问题吗?
-
@MahmoudKhosravi 你能在
view()函数中检查dd(request())的输出吗? -
@sykez 我看到了这个i.stack.imgur.com/xIl8y.png
-
@MahmoudKhosravi 我明白了。您能否验证
dd(request()->all());是否为空。我感觉它是空的,所以你的过滤条件根本没有被触发。