How can I pass parameter in the laravel excel?
Asked Answered
F

2

5

I get tutorial from here : 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

How can I solve this problem?

Framboise answered 28/7, 2018 at 14:17 Comment(0)
M
10

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.

  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:

Define your route as:

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

In your 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');
}
Matthaus answered 28/7, 2018 at 16:9 Comment(1)
Maybe you can help me again. Look at this : #51686386Framboise
G
12

For passing data from controller to laravel excel function we can pass and use data like below

For example, we have to pass data year like 2019 we will pass like below

in controller

Excel::download(new UsersExport(2019), 'users.xlsx');

In laravel import file

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();
    }
}

you can refer all following official documentation link

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

Galvanize answered 21/1, 2021 at 8:50 Comment(1)
Great, this is the best solutionWilding
M
10

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.

  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:

Define your route as:

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

In your 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');
}
Matthaus answered 28/7, 2018 at 16:9 Comment(1)
Maybe you can help me again. Look at this : #51686386Framboise

© 2022 - 2024 — McMap. All rights reserved.