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

knowak001's avatar

Maatwebsite/excel BinaryFileResponse error

Hi, I'm using PHP 7.3.13 and Laravel 6.4.1, Maatwebsite/excel 3.1.

This is my export:

<?php

namespace Edito\Exports;

use Edito\Models\Cms\Event;
use Maatwebsite\Excel\Concerns\FromCollection;

class ParticipantsExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return Event::all();
    }
}

And this is my controller:

    public function getDump()
    {
         return \Excel::download(new ParticipantsExport(), 'dump.xls');
    }

I get this error: "The content cannot be set on a BinaryFileResponse instance.", form this method in BinaryFileResponse class:

    /**
     * {@inheritdoc}
     *
     * @throws \LogicException when the content is not null
     */
    public function setContent($content)
    {
        if (null !== $content) {
            throw new \LogicException('The content cannot be set on 
            a BinaryFileResponse instance.');
        }

        return $this;
    }

The reason is in method setContent(), variable $content instead of null is empty string like this: ''. When I manualy set content to null:

$content = null;

or even comment out whole method, my download works with no erros.

Someone has any idea what can be wrong?

0 likes
17 replies
haztakki's avatar

What is $content? I assume it should throw an exception when it is null not the other way around? You don't actually set anything, just throw an error? @throws \LogicException when the content is not null null may not be sufficient enough, if $content is just empty for example. To check for an empty string, you can do !== "" as well.

1 like
knowak001's avatar

Thanks for reply :)

Variable $content can only be null is this case.

No, method should throw an exception when $content is not null. When I manually pass some other value then null (just for testing) exception is thrown.

This method is from Symfony\Component\HttpFoundation\BinaryFileResponse. I shouldn't change anything in there.

haztakki's avatar

I'm not exactly understanding what you are trying to achieve here, from what I gather, you want to: Export a list of participants providing they exist and are not null. Why not just perform the null check before the export? It doesn't make sense to export a list where it can only be null. Please can you provide some more information.

1 like
knowak001's avatar

I'am trying to achive export of my data from database to xls format for download. I don't need checking if collection I provided is null. I only want export. But when i try to export an exception is thrown from method setContent() in BinaryFileResponse class.

I'am not exactly sure what method setContent() is responsible for, I pasted it here only because of the exception I mentioned. I didn't wrote this, it's from vendor in namespace: Symfony\Component\HttpFoundation\BinaryFileResponse. I only wrote ParticipantExxport and method in controller, and according to Maatwebsite/excel docs it should work.

I am wondering why i get this exception and why variable $content is empty string instead of null (as it should be). $content in this case is not realted to actual content that I want to export from database.

I hope I made it a bit more clear :)

haztakki's avatar

In your collection method, add something to export. For example:

    public function collection()
    {
        return User::all();
    }

Test with something simple. What is Event? What does it give you if you dd it? More information is provided in the documentation. https://github.com/Maatwebsite/laravel-excel-docs/blob/develop/3.1/exports/collection.md

Assuming you have a model named Participant you could do:

    public function collection()
    {
        return Participant::all();
    }
1 like
knowak001's avatar

I actually tested this with something simple which is Event. When i do dd Event::all() returns collection with models of events that useres can attend to. ParticipantsExport should is going to look difrennt. I'm going to pass event instance throughout constructor and then get participants related to event(participants are useres that allready enrolled) but I decided to keep it Simple until it works. As I mentioned before when i change manually in vendor $content to null (which is ofcourse a bad practise but this is just for check) i get exactly what i want.

knowak001's avatar

Yes, it works as expected, but there are diffrent packages versions than i have in my project. I understand how to import excel package and how to use it. I bet that problem is in packages version and the ones I have may cause that problem.

haztakki's avatar

Any reason? I'm using the same Excel version as you. L6/L7 both work. Are you able to stick your code on a repo so I can take a closer look?

1 like
knowak001's avatar

The reason is I'm working on my companys CMS based on laravel and current version is using laravel 6.4.1.

I can't because CMS i mentioned is not MIT.

haztakki's avatar

I can't help you further then. I've provided a working example. It's something you are doing in your code base. Ask another employee for their help.

knowak001's avatar

I allready did, that's why I'm asking here. Anyway, thank you for your time :)

haztakki's avatar

Did you try to simplify the problem down? Use a basic controller and export as shown in the example I provided, with a simple route. If that doesn't work then it's must be something in your code base. I would also reinstall the package in case it is corrupt.

knowak001's avatar

Look at my first post, I don't think it could get any simpler.

I tried reinstalling whole package but it hasn't helped also.

haztakki's avatar

As I said, it's going to be really difficult to help you without seeing more of your code base, as that is where the issue lies. If you can simplify a problem down like the example I did, it proves that it is working. The package also works on L6 so it's not that. I'm not sure how else I can help you. Did you even try moving the working example into your project difficult without editing?

Please or to participate in this conversation.