We use cookies on this site to enhance your user experience

By clicking the Accept button, you agree to us doing so. More info on our cookie policy

Using Laravel Resource Collections with exports

Published: Apr 11, 2021 by C.S. Rhymes

Sometimes you need to get data out of your Laravel app so it can be analysed. There are many ways of doing this, such as exporting rows from the database, or using a package such as Laravel Excel, but this example will utilise Laravel Resource Collections.

In this example we are going to create an export of User information into Excel using Laravel Resource Collection and Spatie’s simple excel package. Laravel Resources are normally used to transform your data for use in an API, but you can also create an array from the resource for use in our export.

The users table has been updated so a user belongs to a company. The companies table has a name for the company in this example.

// App/Models/User.php

public function company()
{
    return $this->belongsTo(\App\Models\Company::class, 'company_id', 'id');
}

Creating the User Resource

To create the user resource we can use the artisan command:

php artisan make:resource UserResource

We can then specify the data we want to include for each user. We will add the name, email and the company name.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
           'name' => $this->name,
           'email' => $this->email,
           'company' => $this->company->name,
        ];
    }
}

Converting the User Resource to an array

When you return the UserResource it will provide you with a json response with the users within the data key. This is perfect for use with an API.

return UserResource::collection(User::get());

Each user is listed with their name, email and company. The keys are what we defined in the UserResource toArray() method so if we wanted to add additional data, for example such as a company phone number that is added in future, then we can update the UserResource and the results will be updated accordingly.

{
    "data": [
        {
            "name": "Zechariah Olson DDS",
            "email": "dreynolds@example.net",
            "company": "Boyle Ltd"
        }
    ]
}

We can modify this statement slightly to provide an array of just the user data we need without the data key by using the resolve() method.

return UserResource::collection(User::get())->resolve();

We can also update the query from User::get() to User::cursor() to return a lazy collection and help reduce the amount of memory when we have many users to export. Cursor allows us to only load the current model we are iterating over, rather than loading all models at once.

return UserResource::collection(User::cursor())->resolve();

Exporting to Excel

Now we are ready to make use of the Simple Excel package from Spatie to convert our array into an Excel file.

composer require spatie/simple-excel

The simple excel package provides a writer to help us easily create an excel file using SimpleExcelWriter::create() but since the file could be very large if our Laravel app has numerous users we can use the SimpleExcelWriter::streamDownload() method instead to allow us to stream the download directly to the browser.

Once we have our writer instance we can add rows of data to the file using the ->addRows() method which expects an array. This is where we pass in our UserResource, with the resolve method so it is converted into an array.

Finally we add ->toBrowser() to return the response to the browser and allow the file to download.

// App/Controllers/UserController.php

public function export()
{
    return SimpleExcelWriter::streamDownload('users-export.xlsx')
        ->addRows(
            UserResource::collection(User::cursor())
                ->resolve()
        )
        ->toBrowser();
}

Other possibilities

Once you have created the resource you can reuse it as it was originally intended as an API response, but you could also use it with another Spatie package called array to xml to convert your array into an XML file instead of a CSV or Excel file.

Photo by Matt Bango from StockSnap

Laravel Exports Resource Collections

Latest Posts

Marketing an ebook with the 4Ps
Marketing an ebook with the 4Ps

I have seen a lot of threads from indie authors asking for advice with marketing their ebook. If I’m honest, I have never really had a strategy for marketing my books, I just assumed that I could put it out there and people would find it. In the real world, that does not seem to be the case.

Building a landing page for your book
Building a landing page for your book

This post follows on from my last post about using your website to promote your ebooks. The first step of the article explains that you need to make a website, but didn’t go into too much detail. This post aims at explaining how you can build a landing page for your book with Bulma Clean Theme.

How to promote your ebooks with your website?
How to promote your ebooks with your website?

I’m a web developer by trade and a part-time author, so here are a few things that I have done to help promote my books and ebooks using my website and my tech know how from my day job.

How NOT to make a website

How NOT to make a Website

By C.S. Rhymes

From £5.49 or read for free on Kindle Unlimited!

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £4.99 or read for free on Kindle Unlimited!