rikless/xporter

CSV wrapper to export eloquent query result

2.0.2 2022-01-04 09:00 UTC

This package is auto-updated.

Last update: 2024-11-04 14:51:23 UTC


README

Xporter is a CSV wrapper allowing you to export Eloquent query results from your laravel application, using league/csv package. With help of Laravels Xporter class creates exportable schema that can be easy modified and reused.

Documentation

Installation

Run:

composer require rikless/xporter:1.*

Example usage

Create a simple class where you'll have all your export parameter anywhere in your application. App\Exports may be a good place to start. You'll need to extend the Rikless\Xporter\Exportable class.

Now complete convert(), query() methods and xportable, rootModel properties.

You have to use the convert() methods to transform your data, and add data from relationship.

In your query method, you just need to instantiate your model and add your query. It can be anything from a Request object in your controller. Be aware to not use the get() or all() method in you query, because the package will chunk results. This mean that it needs an Illuminate\Database\Eloquent\Builder object.

<?php

namespace App\Exports;

use Rikless\Xporter\Exportable;
use Carbon\Carbon;
use Illuminate\Http\Request;

class UsersExporter extends Exportable
{

    protected $xportable = ['email', 'uuid'];

    protected $rootModel = \App\User::class;

    public function convert($item)
    {
        return [
            'email' => $item->email,
            'uuid' => $item->name,
        ];
    }

    public function query(Request $request)
    {
        return (new $this->rootModel)->where('created_at', '<=', Carbon::now());
    }

}

And now in your controller :

<?php

namespace App\Http\Controllers;

use App\Exports\UsersExporter;
use Illuminate\Http\Request;

class TestController extends Controller
{
    public function store(UsersExporter $usersExporter, Request $request)
    {
        return $usersExporter->build($request);
    }
}

License

Licensed under the MIT license.