Download the PHP package real-media-technic-staudacher/laravel-flatfiles without Composer
On this page you can find all versions of the php package real-media-technic-staudacher/laravel-flatfiles. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-flatfiles
Export Eloquent queries to flatfiles using simple configuration
Installation
composer require real-media-technic-staudacher/laravel-flatfiles:^5.0
To overwrite the default configuration
php artisan vendor:publish
Then select the option Provider: RealMediaTechnicStaudacher\LaravelFlatfiles\FlatfileExportServiceProvider
. The default configuration looks like:
As you see, right now, only CSV exports are supported ;-)
Usage example / Basic workflow
Load export
Easiest way is to auto-inject the FlatfileExport
while implementing the FlatfileFields
interface:
If you want to use a dedicated class for field definitions are get the array from somewhere else use withFields()
Specify target file / location
Using filesystem disk
- This enables you to export to all available filesystem drivers
- Exports are generated locally/temporary first and than streamed to disk
Prepare global export resources
...
Loop through data and write to export file
- Preselect the models that will represent a single row in your flat file
- Chunk through this result set to limit resources
Finish export
$flatfile->moveToTarget();
Define fields
Fields are defined within an object/class implementing the FlatfileFields
interface, thus a public function field()
.
Implement this function directly in your export-handling class, or in a dedicated sort like a DTO class.
Why? You get the possiblity to add callbacks in your field definitions, so that you can define dynamic cells easily
In your field()
method you need to define an array of fields.
This will generate a flatfile with one column each field array element.
Assume we load a collection of products having attributes named product_name
. The definition looks like:
The field defintions are pretty flexible. Better learn by examples by yourself
One row for each relation
If you have a relation you want to put into one row and preserve it's parent as one row if it hasn't a relation, you can use the following:
SYLK file format error
By default, an exception getting thrown if the first column in the header row is named ID
.
Background for this is the SYLK formatting, which does not allow an flawless opening with Microsoft Excel in some Versions.
You are free to disable the exception via the config drivers.csv.ignore_sylk_exception
again.
Upgrade guide
To v3 from v2
- Added return types. Also in interfaces. So mainly interfaces have to be checked: ie.
public function fields(): array;
. Tipp: Search forpublic function fields(
across the whole project. - New Namespace. Change imports from
LaravelFlatfiles\*
toRealMediaTechnicStaudacher\LaravelFlatfiles\*
- Changed order of callback paramters of field callback method to prevent
$null
in most of the callsfunction ($null, Asset $asset)
. Now:function (Asset $asset)
. Tipp: Search forfunction ($null
and allfunction fields
in your editor.