我正在try 导出我的数据库中的数据;特别是所有在场的客户,但也是一个选定的客户.
我try 将代码设置为仅当我单击按钮以导出所有客户端时,将保存一个没有名称和扩展名的文件.
ClientsExport Class个
class ClientsExport implements FromCollection
{
private $client = null;
public function __construct($client = null)
{
$this->client = $client;
}
public function collection(Client $client=NULL)
{
if ($this->client) {
return collect([$this->client]);
}
return Client::all();
}
}
try 了几次后,我发现如果我删除了下面代码中的if,那么就不go 管它了
Return Excel::Download(new ClientsExport,‘clients.xlsx’);
它工作正常,所以问题似乎是给了它IF
ClientController个
public function export(Client $client)
{
if($client){
return Excel::download(new ClientsExport($client), $client->surname . ' ' . $client->name . '.xlsx');
} else {
Return Excel::Download(new ClientsExport,‘clients.xlsx’);
}
}
Routes个
Route::get('client-export/{client?}', [ClientController::class,'export'])->name('client.export');
View Blade个
Button where I want to export all clients个
<a class="btn btn-warning mb-5 py-3 px-4 mt-3 me-3 fs-5" href="{{ route('client.export') }}">Export all clients</a>
Button where I want to export the individual client个
<a class="btn btn-warning" href="{{ route('client.export' , compact('client')) }}">Export</a>