All Projects → wilgucki → csv

wilgucki / csv

Licence: MIT License
No description or website provided.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to csv

Admin One Laravel Dashboard
Admin One — Free Laravel Dashboard (Bulma Buefy Vue.js SPA)
Stars: ✭ 94 (+100%)
Mutual labels:  laravel-framework, laravel-package
Laravel Gitscrum
GitScrum is a Project Management Tool, developed to help entrepreneurs, freelancers, managers, and teams Skyrocket their Productivity with the Agile methodology and Gamification.
Stars: ✭ 2,686 (+5614.89%)
Mutual labels:  laravel-framework, laravel-package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+321.28%)
Mutual labels:  laravel-framework, laravel-package
Laraupdater
Enable Laravel App Self-Update. Allow your Laravel Application to auto-update itself.
Stars: ✭ 75 (+59.57%)
Mutual labels:  laravel-framework, laravel-package
Base62
PHP Base62 encoder and decoder for integers and big integers with Laravel 5 support.
Stars: ✭ 16 (-65.96%)
Mutual labels:  laravel-framework, laravel-package
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (+65.96%)
Mutual labels:  laravel-framework, laravel-package
Auth Tests
Always-current tests for Laravel's authentication system. Curated by the community.
Stars: ✭ 230 (+389.36%)
Mutual labels:  laravel-framework, laravel-package
Laravel Open Source Projects
A Web Artisan list of categorized OPEN SOURCE PROJECTS built with Laravel PHP Framework.
Stars: ✭ 676 (+1338.3%)
Mutual labels:  laravel-framework, laravel-package
laravel-jwt-auto-installer
A Laravel Library that let's you add tymon jwt auth library and all it's features with single command
Stars: ✭ 19 (-59.57%)
Mutual labels:  laravel-framework, laravel-package
laravel-smart-facades
Strategy design pattern in laravel, the easiest way.
Stars: ✭ 84 (+78.72%)
Mutual labels:  laravel-framework, laravel-package
Backup
MySQL Database backup package for Laravel
Stars: ✭ 66 (+40.43%)
Mutual labels:  laravel-framework, laravel-package
artisan-shortcuts
🍰 Register shortcuts to execute multiple artisan commands
Stars: ✭ 56 (+19.15%)
Mutual labels:  laravel-framework, laravel-package
Laravel Compass
A REST client inside your Laravel app
Stars: ✭ 1,002 (+2031.91%)
Mutual labels:  laravel-framework, laravel-package
Dropzone Laravel Image Upload
Laravel 5.2 and Dropzone.js auto image uploads with removal links
Stars: ✭ 92 (+95.74%)
Mutual labels:  laravel-framework, laravel-package
Laravel Qrcode Ecommerce
This is a complete laravel project that handles qrcodes, payments, api/microservices, and ecommerce
Stars: ✭ 36 (-23.4%)
Mutual labels:  laravel-framework, laravel-package
Wagonwheel
Offer an online version of your Laravel emails to users.
Stars: ✭ 224 (+376.6%)
Mutual labels:  laravel-framework, laravel-package
Laravel Code Generator
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.
Stars: ✭ 485 (+931.91%)
Mutual labels:  laravel-framework, laravel-package
Snooze
A package to simplify automating future notifications and reminders in Laravel
Stars: ✭ 515 (+995.74%)
Mutual labels:  laravel-framework, laravel-package
Laravel User Activity
Monitor user activity easily!
Stars: ✭ 253 (+438.3%)
Mutual labels:  laravel-framework, laravel-package
laravel-crm
Free & Opensource Laravel CRM solution for SMEs and Enterprises for complete customer lifecycle management.
Stars: ✭ 927 (+1872.34%)
Mutual labels:  laravel-framework, laravel-package

CSV

Laravel 5 package for reading and writing CSV files.

Warning

The package has been updated to PHP 7. If you can't update to PHP 7 use version 0.6.x

Instalation

Install package with composer

composer require wilgucki/csv

For projects built with Laravel 5.5+ you can ommit next step. Package uses package discovery feature so you don't need to modify config file. For older Laravel versions you will need to add service provider and aliases to config/app.php file

'providers' => [
    //... 
    Wilgucki\Csv\CsvServiceProvider::class,
]

// ...

'aliases' => [
    //...
    'CsvReader' => Wilgucki\Csv\Facades\Reader::class,
    'CsvWriter' => Wilgucki\Csv\Facades\Writer::class,
]

Last step is to publish package config

php artisan vendor:publish --provider="Wilgucki\Csv\CsvServiceProvider"

Usage

Config file

Package config can be found in csv.php file under config directory (after you have published it). The file contains default values for delimiter, enclosure and escape parameters. You can set default values here and skip passing additional parameters to open and create methods (we discuss them later).

Convert encoding

Common issue when working with CSV files generated by Excel is encoding. Excel exports CSV file encoded with windows-1250 character set while most of PHP applications use UTF-8. To solve this issue you can set encoding option in the config file. You can set your encoding preferences separately for reader and writer.

'encoding' => [
    'reader' => [
        'enabled' => true,
        'from' => 'CP1250',
        'to' => 'UTF-8'
    ],
    'writer' => [
        'enabled' => true,
        'from' => 'UTF-8',
        'to' => 'CP1250'
    ]
]

As you can see in the example above, Reader will convert windows-1250 encoding to UTF-8, while Writer will do this in opposite way. You don't have to use both options. You can set encoding conversion only for one class - reader or writer.

Reader

Start with opening CSV file.

$reader = CsvReader::open('/path/to/file.csv');

If you need to change delimiter, enclosure or escape you can do it by passing proper values to open method. More information about these values can be found here - http://php.net/manual/en/function.fgetcsv.php.

$reader = CsvReader::open('/path/to/file.csv', ';', '\'', '\\\\');

Having your CSV file opened you can read it line after line

while (($line = $reader->readLine()) !== false) {
    print_r($line);
}

or you could read whole file at once

print_r($reader->readAll());

If your CSV file contains header line, you can convert it into array keys for each line.

$reader = CsvReader::open($file, ';');
$header = $reader->getHeader();
print_r($header);
print_r($reader->readAll());

Don't forget to close file after you're done with your work.

$reader->close();

Writer

Create new CSV file

$writer = CsvWriter::create('/path/where/your/file/will/be/saved.csv');

File path is optional. If you won't provide it CsvWriter will use memory as a storege.

If you need to change delimiter, enclosure or escape you can do it by passing proper values to create method. More information about these values can be found here - http://php.net/manual/en/function.fputcsv.php.

$writer = CsvWriter::create('/path/to/file.csv', ';', '\'', '\\\\');

To add data into CSV file you can use writeLine or writeAll methods.

$writer->writeLine(['some', 'data']);
$writer->writeLine(['another', 'line']);

$writer->writeAll([
    ['some', 'data'],
    ['another', 'line'],
]);

To display data added to CSV file use flush method.

echo $writer->flush();

Don't forget to close file after you're done with your work.

$writer->close();

Integrating with Eloquent models

If you want/need to integrate CsvReader and/or CsvWriter with Eloquent model there's a simple way of doing this. The Csv package provides three traits in order to simplify the process.

These traits hasn't been tested for handling relations. Relations are still on a TODO list

CsvCustomCollection

CsvCustomCollection trait added to model class enables toCsv method that can be used on a collection.

use Wilgucki\Csv\Traits\CsvCustomCollection;

class SomeModel extends Model
{
    use CsvCustomCollection;
    
    //...
}

$items = SomeModel::all();
$csvData = $items->toCsv();

CsvExportable

CsvExportable trait allows you to convert single model object to CSV data.

use Wilgucki\Csv\Traits\CsvExportable;

class SomeModel extends Model
{
    use CsvExportable;
    
    //...
}

$csvData = SomeModel::find(1)->toCsv();

CsvImportable

CsvImportable trait allows you to import data from CSV file and save it to the database. Imported file must have header line containing column names as they are named in database table. Primary key must be named id. CSV importer will update all rows with matching ids and add every row that isn't found in a table.

Each column from CSV file is checked against $fillable array, letting to insert or update only these columns that are present in it.

use Wilgucki\Csv\Traits\CsvImportable;

class SomeModel extends Model
{
    use CsvImportable;
    
    //...
}

SomeModel::fromCsv('/path/to/file.csv');

Command line

csv:import

To import CSV file into database table, use csv:import command.

php artisan csv:import model csv-file

  • model - model class name with its namespace
  • csv-file - file name with path relative to project's root directory

If you would like to import users, you could use command like this (remember to use correct CSV file path)

php artisan csv:import "App\User" storage/users.csv

csv:export

This command allows you to export data from database table into CSV file.

php artisan csv:export model csv-file

  • model - model's class name with its namespace
  • csv-file - file name with path relative to project's root directory

If you would like to export users, you could use command like this (remember to use correct CSV file path)

php artisan csv:export "App\User" storage/users.csv

TODO

  • handle relations
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].