0

I'm trying to export to Excel from Query but give me an error

Too few arguments to function App\Exports\JobExport::__construct(), 0 passed and exactly 1 expected

App\Exports:

use App\Applyed;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class JobExport implements FromQuery
{
 use Exportable;
public function __construct(int $id)
{
    $this->id = $id;
    return $this;
}

public function query()
{
    return Applyed::query()->whereId('job_id', $this->id);
}
}

Controller:

 public function export($id)
{  
    return (new JobExport)->forId($id)->download('invoices.xlsx');
}  

Route:

Route::get('job/export/{id}', 'JobsController@export');

Blade:

<a href="{{url('job/export',$job->id)}}" class="button big ripple-effect">Export to Excel</a>
Hima Hima
  • 123
  • 16
  • [This helps me a lot.](https://stackoverflow.com/questions/56104727/laravel-excel-3-1-add-extra-rows-betweem-iterations) hope it's helps you also. – Hima Hima Jul 20 '19 at 11:44

1 Answers1

0

Change

return (new JobExport)->forId($id)->download('invoices.xlsx');

Into

return (new JobExport($id))->download('invoices.xlsx');

Or remove the $id constructor argument if you want to use the forId() setter instead.

PtrTon
  • 3,705
  • 2
  • 14
  • 24
  • thank you for your comment, I used return (new JobExport($id))->download('invoices.xlsx'); but give me empty excel :( – Hima Hima Jul 20 '19 at 09:05