如何在laravel excel中传递参数? [英] How can I pass parameter in the laravel excel?

查看:474
本文介绍了如何在laravel excel中传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里获得教程: 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为空

I want to pass id parameter to export file. I try like that, but I don't get the id. The id is null

我该如何解决这个问题?

How can I solve this problem?

推荐答案

不幸的是,当您具有特定参数时,不能使用常规依赖项注入.这是您可以做的:

Unfortunately you can't use normal dependency injection when you have a specific parameter. This is what you can do though:

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,但是有两种解决方法.

Now the problem is that the container doesn't know how to resolve $id however there are two ways around this.

  1. 手动传递$id:

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

  • 路线注入:

  • 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'));   
         });
    }
    

    然后将您的路线方法简化为:

    Then your route method is simplified as:

    public function exportToExcel(ItemsDetailsExport $itemExport)
    {
        //It will be injected based on the parameter you pass to the route
        return $itemExport->download('Summary Detail.xlsx');
    }
    

    这篇关于如何在laravel excel中传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆