代码之家  ›  专栏  ›  技术社区  ›  Success Man

如何在Laravel Excel中传递参数?

  •  2
  • Success Man  · 技术社区  · 7 年前

    我从这里得到了指导: 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);
        }
    }
    

    1 回复  |  直到 7 年前
        1
  •  4
  •   apokryfos    7 年前

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

    1. $id

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

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

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