M.Izzat
M.Izzat

Reputation: 1166

Laravel Excel passing argument

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

Upvotes: 0

Views: 1585

Answers (1)

Franz
Franz

Reputation: 354

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

Upvotes: 2

Related Questions