moses toh
moses toh

Reputation: 13192

How can I pass parameter in the laravel excel?

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?

Upvotes: 5

Views: 24509

Answers (2)

Sanmit Pawar
Sanmit Pawar

Reputation: 243

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

Upvotes: 12

apokryfos
apokryfos

Reputation: 40690

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

Upvotes: 10

Related Questions