laravel 如何在laravel excel中传递参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51572197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass parameter in the laravel excel?
提问by Success Man
I get tutorial from here : https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics
我从这里得到教程: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');
}
}
My export like this :
我的出口是这样的:
<?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);
}
}
I want to pass id parameter to export file. I try like that, but I don't get the id. The id is null
我想将 id 参数传递给导出文件。我尝试这样做,但我没有得到 id。id 为空
How can I solve this problem?
我怎么解决这个问题?
回答by apokryfos
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);
}
}
Now the problem is that the container doesn't know how to resolve $id however there are two ways around this.
现在的问题是容器不知道如何解析 $id 但是有两种方法可以解决这个问题。
Manual passing of
$id
:public function exportToExcel($id) { $exporter = app()->makeWith(ItemsDetailsExport::class, compact('id')); return $exporter->download('Summary Detail.xlsx'); }
Route injection:
手动传递
$id
:public function exportToExcel($id) { $exporter = app()->makeWith(ItemsDetailsExport::class, compact('id')); return $exporter->download('Summary Detail.xlsx'); }
路由注入:
Define your route as:
将您的路线定义为:
Route::get('/path/to/export/{itemExport}', 'ItemController@exportToExcel');
In your RouteServiceProvider.php
:
在您的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');
}