Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
1.0 kB
2
Indexable
Never
namespace App\Exports;

use App\Models\YourModel;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\Exportable;

class LargeDataExport implements FromView, WithChunkReading, ShouldAutoSize
{
    use Exportable;

    protected $page;

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

    /**
     * Pass the data to the view in chunks.
     */
    public function view(): View
    {
        // Fetch data in chunks based on the current page
        $data = YourModel::query()
            ->skip(($this->page - 1) * $this->chunkSize())
            ->take($this->chunkSize())
            ->get();

        return view('exports.large-data', [
            'data' => $data,
        ]);
    }

    /**
     * Define chunk size.
     */
    public function chunkSize(): int
    {
        return 1000;
    }
}
Leave a Comment