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

WhisperingWolf's avatar

Laravel Excel skip and take not working

Hello everyone thank you for your time firstly,

I was working with Laravel excel to export data from my database to excel, everything was going well but right now I am stuck on a small problem, I dont want the user to be able to export the entire table at once I want to allow him to export only 10-25-50-100 per time, and at the same time i have pagination so if the user is in page 2 i want to get only 25 records from page 2, this is how i was attempting:

return $query->skip(($this->page - 1) * $this->perPage)
            ->take($this->perPage);

This works but it just exports the entire table without perPage/pagination(and i did test perPage and page do come correctly i did dd them)

Thank you for your answers.

0 likes
4 replies
Snapey's avatar

Better explaining how you are exporting.

1 like
WhisperingWolf's avatar

@Snapey,

I apologize for so little explanation I will give more info and show the rest of the code down below.

I am using Laravel Excel page to export some of my tables to excel for users to download, I have created export files for each table that I needed with the cmd command, also I did manage to fix the problem but I am still not completely sure that my approach is the correct one so I will share my code:

	protected $query;
    protected $perPage;
    protected $page;

    public function __construct($query, $perPage, $page)
    {
        $this->query = $query;
        $this->perPage = $perPage;
        $this->page = $page;
    }

public function collection()
    {
        $query = MachineRepair::select(
            task_code',
            'priority',
            'description',
            'created_at'
        );

        if ($this->query) {
            $query->where(function ($query) {
                $query->where('task_code', 'like', "%{$this->query}%")
                    ->orWhere('description', 'like', "%{$this->query}%");
            });
        }
        
        // Paginate results
        $results = $query->skip(($this->page - 1) * $this->perPage)
            ->take($this->perPage)
            ->get();

        // Return paginated collection
        return $results;
    }

So to begin with i was using FromQuery (query) approach but that didnt seem to work so i switched to FromCollection which exports data from a laravel collection and that solved the issue, I miss-understood what I needed to do but after reading the documentation I figured out what i was doing wrong. So the FromQuery doesn't accept laravel collection to fill up the excel sheet but FromCollection does. (I might be wrong and miss-understanding it).

Thank you for your time @snapey please feel free to give any ideas if there are any better ways to fix the problem.

Snapey's avatar
Snapey
Best Answer
Level 122

@WhisperingWolf in your collection method you start off using $query but then switch to $this->query

1 like
WhisperingWolf's avatar

@Snapey,

Yes, I have the $this->query is the search that the user can do which i get in controller from $search = $request->query('query', ''); but I am going to switch that to $this->search so its cleaner and less confusing for other people.

Please or to participate in this conversation.