如何在Laravel Excel中传递参数?

5
我从这里获取教程:https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics
<?php
...
use App\Exports\ItemsDetailsExport;
class ItemController extends Controller
{
    ...
    public function exportToExcel(ItemsDetailsExport $exporter, $id)
    {
        //dd($id); I get the result
        return $exporter->download('Summary Detail.xlsx');
    }
}

我的导出结果如下:
<?php
namespace App\Exports;
use App\Repositories\Backend\ItemDetailRepository;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Support\Facades\Input;
class ItemsDetailsExport implements FromCollection
{
    use Exportable;
    protected $itemDetailRepository;
    public function __construct(ItemDetailRepository $itemDetailRepository)
    {
        $this->itemDetailRepository = $itemDetailRepository;
    }
    public function collection()
    {
        $test  = Input::get('id');
        dd('yeah', $test);
    }
}

我想将id参数传递给导出文件。我尝试了这样做,但我没有获得id值。id的值为空。请问如何解决这个问题?
2个回答

11

要将数据从控制器传递给Laravel Excel函数,我们可以像下面这样传递和使用数据

例如,我们需要传递年份数据,比如2019,我们会像下面这样传递

在控制器中

Excel::download(new UsersExport(2019), 'users.xlsx');
在 Laravel 中导入文件。
class UsersExport implements FromCollection {
    private $year;

    public function __construct(int $year) 
    {
        $this->year = $year;
    }
    
    public function collection()
    {
        return Users::whereYear('created_at', $this->year)->get();
    }
}

你可以参考以下所有官方文档链接

https://docs.laravel-excel.com/3.1/architecture/objects.html#plain-old-php-object


太好了,这是最佳解决方案。 - Freddy Daniel

10

很遗憾,当您有一个特定参数时,无法使用普通的依赖注入。但是您可以这样做:

class ItemsDetailsExport implements FromCollection
{
    use Exportable;
    protected $itemDetailRepository;
    protected $id;
    public function __construct(ItemDetailRepository $itemDetailRepository, $id)
    {
        $this->itemDetailRepository = $itemDetailRepository;
        $this->id = $id; 
    }
    public function collection()
    {
        $test  = $this->id;
        dd('yeah', $test);
    }
}

现在的问题是容器不知道如何解析 $id,但有两种解决方法。
  1. Manual passing of $id:

    public function exportToExcel($id)
    {
        $exporter = app()->makeWith(ItemsDetailsExport::class, compact('id'));   
        return $exporter->download('Summary Detail.xlsx');
    }
    
  2. Route injection:

将您的路由定义为:

 Route::get('/path/to/export/{itemExport}', 'ItemController@exportToExcel');

在您的 RouteServiceProvider.php 文件中:
public function boot() {
     parent::boot();
     //Bindings

     Route::bind('itemExport', function ($id) { //itemExport must match the {itemExport} name in the route definition
         return app()->makeWith(ItemsDetailsExport::class, compact('id'));   
     });
}

那么你的路由方法可以简化为以下内容:
public function exportToExcel(ItemsDetailsExport $itemExport)
{
    //It will be injected based on the parameter you pass to the route
    return $itemExport->download('Summary Detail.xlsx');
}

也许你可以再帮我一次。看看这个:https://stackoverflow.com/questions/51685886/how-can-i-add-title-and-sum-value-of-column-in-laravel-excel-version-3 - moses toh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接