Importables

In the previous example, we used the Simplesheet::import facade to start an import.

Laravel Simplesheet also provides a Nikazooz\Simplesheet\Concerns\Importable trait, to make import classes importable.

namespace App\Imports;

use App\User;
use Nikazooz\Simplesheet\Concerns\ToModel;
use Nikazooz\Simplesheet\Concerns\Importable;

class UsersImport implements ToModel
{
    use Importable;

    public function model(array $row)
    {
        return new User([
            'name' => $row[0],
        ]);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Importing

We can now import without the need for the facade:

(new UsersImport)->import('users.xlsx', 'local', \Nikazooz\Simplesheet\Simplesheet::XLSX);
1

Queuing

Or queue the import:

(new UsersImport)->queue('users.xlsx');
1

To array

The import can be loaded into an array :

$array = (new UsersImport)->toArray('users.xlsx');
1

To collection

The import can be loaded into a collection:

$collection = (new UsersImport)->toCollection('users.xlsx');
1