0

I have gone through the docs and also Googled. I see little mention of returning multiple queries on the same sheet from Maat's Laravel Excel. I presume therefore it is 1 query for 1 downloaded spreadsheet. I also presume that if you do have multiple queries that you will need to place each query on an additional sheet.

Have got this right ?

Many thanks

Vince
  • 1,405
  • 2
  • 20
  • 33

1 Answers1

2

In a perfect world, every query would get its own sheet. But in reality, it will export whatever you give it so long as it receives a single array or collection for the output, depending on your configuration. It would be up to you to determine how to combine your queries into a format that could be interpreted as rows and columns.

Basic example with two queries:

class ExportSample implements FromCollection
{
    // ...

    public function collection()
    {
        // query 1
        $a = User::where('id',2)->get();

        // query 2
        $b = User::where('id',4)->get();

        // merge collections
        return $a->merge($b);
    }    
}

Of course, if your queries result in different column structures, there may be additional obstacles.

matticustard
  • 4,850
  • 1
  • 13
  • 18
  • Thank you for your response. I discovered the merge approach after posting and found that in my use cases it will not work, as one query aggregates the values in a 'GroupBy', but in the other query they are not aggregated. And so, the merged collection does not work in both cases. Essentially what I am trying to do is when the user clicks the single "report" link, it delivers somehow, two data sets. They are related, but different. btw - with a name like Matticustard and your BIO, my bet is you could be Matt Stauffer in disguise :o) If you get a brain wave please let me know. Thanks – Vince Feb 12 '21 at 22:03
  • Ha! Flattered, but not Matt Stauffer. It's certainly possible to have a single report link that returns one file with multiple sheets, or even everything on a single sheet. The output is only limited by the amount of effort you're willing to put into organizing your data into rows and columns. A mock-up of your desired output would go a long way toward offering more specific thoughts on how you could proceed. – matticustard Feb 12 '21 at 22:17
  • This is quite tricky on SO. Things would be easier if you have an account on Laracasts. Nevrtheless, I have provided a screen shot of a spreadsheet table with some info. Here https://drive.google.com/file/d/1G-9HkCLipoykP1tnDvVjAndjVi6H5lOW/view?usp=sharing The table in green works and is in place. I can't really change this as this is what the user demands. You will notice that the values of the children are aggregated eg Morgan and Sarah. The table below is what I need. You will notice the values are not aggregated for Sarah and Morgan. There in lies one of the problems. Many thanks!!! – Vince Feb 14 '21 at 01:25
  • 1
    To do what is seen in the image, you must manually generate the table structure. I put together a simple Gist which demonstrates this concept. It's easier to understand with arrays, so I changed the export method. I guessed at some of the variable names, but I think you'll get the idea. https://gist.github.com/matticustard/53cab46a9085c2f46785434045f78745 – matticustard Feb 14 '21 at 04:02
  • You are a rock star! Thank you very much. It being Sunday, the wife has all kinds of stuff I need to do, so I will give this a good look tomorrow. With you being in the top 6% of SO, you should be on Laracasts on the top of the leader board! You will see me there. I'll give you feed back tomorrow. Many thanks ! – Vince Feb 14 '21 at 17:31
  • Finally got to your code. Added long comment and code to Github. Many thanks! – Vince Feb 18 '21 at 18:27
  • I'm glad I could help! I added one more snippet in the comments on Github based on your desire for totals. – matticustard Feb 18 '21 at 23:01
  • not sure if github automatically sends notifications, so letting you know I posted there. – Vince Feb 25 '21 at 03:51
  • Replied. I do get notifications from GitHub. If you do as well, we can stop notifying each other here. Thanks. – matticustard Feb 25 '21 at 05:13