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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great, this is the best solution
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');
}

1 Comment

Maybe you can help me again. Look at this : stackoverflow.com/questions/51685886/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.