PHPackages                             lws/import - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lws/import

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lws/import
==========

:description

0.1(6y ago)482licensePHP

Since Jul 16Pushed 6y ago7 watchersCompare

[ Source](https://github.com/ladybirdweb/laravel-importer)[ Packagist](https://packagist.org/packages/lws/import)[ Docs](https://github.com/lws/import)[ RSS](/packages/lws-import/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (8)Versions (4)Used By (0)

Laravel Importer
================

[](#laravel-importer)

Flexible and reliable way to import, parse, validate and transform your csv,xsl,xslx,JSON &amp; XML files with laravel

[![Build Status](https://camo.githubusercontent.com/f93e1de62663ab42bbcef20c125cf821beca9d14ee139e914cc1204199b3867e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c616479626972647765622f6c61726176656c2d696d706f727465722f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ladybirdweb/laravel-importer/build-status/master) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/ba2525f2e66a149e14c997333dee0ee0e55188aebdefdcbea9b714d156f2cd61/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c616479626972647765622f6c61726176656c2d696d706f727465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ladybirdweb/laravel-importer/?branch=master) [![Build Status](https://camo.githubusercontent.com/c61f09efb32ad135e456a41761f13bc04794617fb49c235c60647c9beb273a8c/68747470733a2f2f7472617669732d63692e6f72672f6c616479626972647765622f6c61726176656c2d696d706f727465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ladybirdweb/laravel-importer) [![StyleCI](https://camo.githubusercontent.com/82d1545d40c836472f8e0ecbb77013143b1daff074c56c8f9dabd3dec87dc589/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3139303836333337322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/190863372) [![Code Intelligence Status](https://camo.githubusercontent.com/915a76cb534e6327c7cbb00638f7eec056b289ed1c61caa3e0d694f01a3f2df1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c616479626972647765622f6c61726176656c2d696d706f727465722f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

Installation
------------

[](#installation)

Via Composer

```
$ composer require lws/import
```

Usage
-----

[](#usage)

After installation you need to add the following line to config/app.php (No Need if Laravel &gt;= 5.5)

```
'providers' => [
	/*
     * Package Service Providers...
     */
    LWS\Import\ImportServiceProvider::class,
]

```

After setup you need migrate the database using

```
$ php artisan migrate
```

Change QUEUE\_CONNECTION entry in .env file

```
QUEUE_CONNECTION=database

```

The model you wish to import data to, should implement `RelationshipsTrait` in order to obtain the relationships associated with the model and relationships can be obtained by `$modelObj->relationships()` as an Array.

```
use LWS\Import\Trait\RelationshipsTrait;

class User extends Model {
	use RelationshipsTrait;
}
```

This package has a Base Controller `Import` that you have to extend in your Controller

```
use LWS\Import\Import;

class YourController extends Import
{
	//Magic
}
```

`Import` class have the following methods:

- `parseImport($source,Request $request)` : It accepts a request containing the CSV file that needs to be imported as first parameter and the second parameter is name of the model,for which the importing has to be perfomed.It returns the database columns associated with the model and sample row from csv file for mapping. Name of the csv file should be `csv_file`.
- `processImport(Closure $callback)` : It accepts a `Closure` which has the logic of mapping database columns to records in csv file.The Closure has to be written by user.
- `getModelObject($source)` : Accepts a model name and returns the model object if it is present in your application,otherwise return error response.

This Package uses a `Factory` Class to resolve the model which sent as parameter the default namespace from which the `Factory` tries to resolve model classes is `App`. If your models are in different namespace you can specify the namespace to look for resolving in `config/import.php`.

Example Controller:

```
