Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Slithu's avatar

The value is not properly passed to the export class

I have a form to display borrowings after entering the year the book was published

<form action="{{ route('booking.GetBookingsByYear') }}" method="POST">
            @csrf
            <input type="text" name="year" value="{{ old('year') }}" placeholder="Enter Book Year" class="form-control">
            <button type="submit" class="btn btn-primary btn-sm col-12">Filter</button>
</form>

And I want to export data for a given year to an excel file. This is my class export code:

<?php

namespace App\Exports;

use App\Models\Booking;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class GetBookingsByYearExport implements FromCollection
{
    protected $year;

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

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $results = DB::select('CALL GetBookingsByYear(?)', [$this->year]);
        return collect($results);
    }
}

I'm using a MySQL procedure to display the borrowings for a book by specifying the year. This procedure:

DELIMITER //

CREATE PROCEDURE GetBookingsByYear(IN bookYear INT)
BEGIN
  SELECT b.*
  FROM bookings AS b
  JOIN books AS bk ON b.bookId = bk.id
  WHERE bk.year = bookYear;
END //

DELIMITER ;

Controller Function:

public function GetBookingsByYearexport(Request $request)
    {
        $year = $request->input('year');
        $export = new GetBookingsByYearExport($year);
        return Excel::download($export, 'bookingyear.xlsx');
    }

Routing:

Route::any("/booking/GetUserBookingsexport/", [BookingController::class, 'GetUserBookingsexport'])->name('booking.GetUserBookingsexport');

And the problem is that it exports the excel file but it is empty. After typing dd($this->year); in the collection() function in the Export class, I get this: null // app\Exports\GetBookingsByYearExport.php:24. The year value is not passed correctly. How to fix it?

0 likes
0 replies

Please or to participate in this conversation.