我正在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> 

推荐答案

问题是,当使用route model binding时,将控制器方法定义为:public function export(Client $client)将为\App\Models\Client提供一个新的"empty"实例,而路由定义中的client id"empty".因此,下面在您的控制器方法中的判断always将是真实的.

ClientController::export(\App\Models\Client $client)

       if($client){
            // ...
        } else {
            // ...  
        } 

要解决这个问题,您必须将控制器方法的参数设置为NULL.这将指示Laravel的路由模型绑定系统为您提供NULL $client变量,而不是当您的路由定义中的client id"empty"时,Client模型(new \App\Models\Client)的"空"实例.即:

ClientController

// ...
    public function export(\App\Models\Client $client = null)
    {

    }
// ...

FULL SOLUTION

ClientsExport


class ClientsExport implements FromCollection
{
    public function __construct(private ?\App\Models\Client $client = null)
    {
    }

    public function collection()
    {
        return is_null($this->client)
            ? Client::all()
            : collect([
                $this->client
            ]);
    }

}

ClientController

// ...
    public function export(\App\Models\Client $client = null)
    {
        return Excel::download(new ClientsExport($client),
            is_null($client)
                ? 'clients.xlsx'
                : $client->surname . ' ' . $client->name . '.xlsx'
        );
    }
// ...

Laravel相关问答推荐

Hostinger Laravel网站未显示错误

如何判断用户是否已登录Laravel

AJAX响应中未定义的全名

RuntimeException 未安装 Zip PHP 扩展

允许的内存大小 134217728 字节用尽(试图分配 20480 字节) Laravel

如何在 laravel 中安装 PHPExcel 库?

Laravel 5.x 中 onUpdate / onDelete 的可用操作

在 Laravel 5 中扩展请求类

Laravel 5 - 在控制器文件中为多个路由定义中间件

Laravel 动作未定义

Laravel Eloquent:计算总价的最佳方法

在Lumen框架中启用会话

Laravel assets资源与混合

脚本 php artisan clear-compiled 处理 pre-update-cmd 事件返回错误(Laravel 4.1 升级)

Laravel php artisan 产生错误

Laravel 单元测试依赖注入

如何判断是否连接到 Laravel 4 中的数据库?

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)

如何在 Laravel 5.3 中获取当前的语言环境

Laravel如何仅响应没有正文消息的204代码状态