0

I'm trying to pass argument to filter the data I want to export to Excel.

This is my code :

class UnitExport implements FromCollection
{
    public function collection($proj_id)
    {
        return Unit::where('project_id', $proj_id);
    }
}


class UnitController extends Controller
{
    public function index($proj_id)
    {

        return view('dev-admin.projects.units.index', ['proj_id' => $proj_id]);

    }

    public function unitExcelExport($proj_id)
    {

        return Excel::download(new UnitExport($proj_id), 'Unit.xlsx');

    }
}

When try this it says i receive an error says:

Declaration of App\Http\Controllers\Developer\Admin\UnitExport::collection($proj_id) must be compatible with Maatwebsite\Excel\Concerns\FromCollection::collection()

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
M.Izzat
  • 1,086
  • 4
  • 22
  • 50
  • I believe the collection function in your UnitExport class should not have any parameter and the $proj_id should be used as the constructor argument not the collection function. In your code, usage of "new UnitExport($proj_id)" seems not to be correct – Ali Khalili Oct 02 '19 at 04:29
  • you should pass $proj_id to the constructor – Franz Oct 02 '19 at 05:11

1 Answers1

2

You can't pass your argument directly to your collection function. Try this.

class UnitExport implements FromCollection
{
    protected $proj_id;

    public function __construct($proj_id)
    {
       $this->proj_id = $proj_id;
    }

    public function collection()
    {
        return Unit::where('project_id', $this->proj_id)->get();
    }
}
Franz
  • 354
  • 1
  • 4
  • 15