Skip to content

Commit

Permalink
Allow for an array of headers to be defined in the model instead of a…
Browse files Browse the repository at this point in the history
…s sheet row
  • Loading branch information
edgrosvenor committed Mar 28, 2020
1 parent 1e0c9ad commit e217877
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![StyleCI](https://github.styleci.io/repos/244386505/shield?branch=master)](https://github.styleci.io/repos/244386505)
![Build Status](https://app.chipperci.com/projects/65d88e3d-ecf4-4ced-afd3-e5ba3593ce21/status/master)

This package provides an Eloquent model that sits on top of a Google Sheet. In order for it to work, there are two things your sheet needs to have. One is a heading row that holds the name of your columns. This defaults to row 1 (the top row) but it can be any row in the sheet. The other is a primary key column. Eloquent assumes that your primary key column is named id. If it's not, set it in your model like you would normally.
This package provides an Eloquent model that sits on top of a Google Sheet. You can use a heading row that holds the name of your columns. This defaults to row 1 (the top row) but it can be any row in the sheet. Or you can define an array of headings in your model. If you choose not to have a primary key column in your spreadsheet, each column will get an id (or whatever you're calling your primary key column in your model) auto-incremented.

When you use this package, an initial invocation of the model will read the sheet and store each row as a record in a table inside a file-based sqlite database. Subsequent invocations of the model use that sqlite database so changes to the spreadsheet won't be reflected in the database. However, there are two ways that you can invalidate the sqlite cache and cause it to be recreated:

Expand Down
3 changes: 2 additions & 1 deletion src/SheetModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SheetModel extends Model
protected $headerRow;
public $primaryKey = 'id';
public $cacheName;
protected $headers;

public function __construct()
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public function loadFromSheet(): array

$sheet = $sheets->spreadsheet($this->spreadsheetId)->sheetById($this->sheetId)->get();

$headers = collect($sheet->pull($this->headerRow - 1));
$headers = is_array($this->headers) ? collect($this->headers) : collect($sheet->pull($this->headerRow - 1));

if (!$headers->contains($this->primaryKey)) {
$headers->push($this->primaryKey);
Expand Down
13 changes: 13 additions & 0 deletions tests/Models/DefineHeadersModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests\Models;

use Grosv\EloquentSheets\SheetModel;

class DefineHeadersModel extends SheetModel
{
protected $spreadsheetId = '1HxNqqLtc614UVLoTLEItfvcdcOm3URBEM2Zkr36Z1rE';
protected $sheetId = '282825363';
protected $headerRow = '1';
protected $headers = ['id', 'name', 'email'];
}
11 changes: 11 additions & 0 deletions tests/SheetModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Illuminate\Support\Facades\File;
use Tests\Models\DefineHeadersModel;
use Tests\Models\InferredIdModel;
use Tests\Models\TestModel;

Expand Down Expand Up @@ -63,6 +64,16 @@ public function can_infer_id_from_row()
$this->assertEquals('[{"name":"Ed","email":"ed@gros.co","id":1},{"name":"Justine","email":"justine@gros.co","id":2},{"name":"Daniel","email":"daniel@gros.co","id":3},{"name":"Milo","email":"milo@gros.co","id":4}]', $sheet->toJson());
}

/** @test */
public function can_use_defined_headers()
{
$sheet = DefineHeadersModel::find(1);

$this->assertEquals('Ed', $sheet->name);


}

/** @test */
public function can_invalidate_cache()
{
Expand Down

0 comments on commit e217877

Please sign in to comment.