Skip to content

Commit

Permalink
Covid 19 for tinkerwell
Browse files Browse the repository at this point in the history
  • Loading branch information
BSN4 committed Mar 30, 2020
1 parent 8bb9feb commit f99c13c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
69 changes: 69 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

### Covid 19 Example

```php
class Covid19Date extends \Illuminate\Database\Eloquent\Model
{
use \Awssat\Kabsa\Traits\Kabsa;

}


class Covid19 extends \Illuminate\Database\Eloquent\Model
{
use \Awssat\Kabsa\Traits\Kabsa, \Awssat\Kabsa\Traits\KabsaRelationships;

protected static $total = 0;

public function getRows()
{
return collect(Http::get('https://pomber.github.io/covid19/timeseries.json')
->json())
->map(function ($dates, $country) {

foreach($dates as $date) {
$date['country'] = $country;
Covid19Date::addRow($date);
}

static::$total += end($dates)['confirmed'] ?? 0;

return ['name' => $country];
})->toArray();
}

public function timeline()
{
return $this->hasManyKabsaRows(Covid19Date::class, 'country', 'name');
}

public function getTotalAttribute()
{
return $this->timeline->last()->confirmed;
}

public function yesterday()
{
return $this->timeline->firstWhere('date', now()->subDay()->format('Y-n-d'));
}

public function today()
{
return $this->timeline->firstWhere('date', now()->format('Y-n-d'));
}

public function lastWeek()
{
return $this->timeline->firstWhere('date', now()->subWeek()->format('Y-n-d'));
}

public static function totalAroundTheWorld()
{
return static::$total;
}
}

Covid19::totalAroundTheWorld();

Covid19::firstWhere('name', 'United Kingdom')->lastWeek()->confirmed;
```
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ To get started with Laravel Kabsa, use Composer to add the package to your proje
composer require awssat/laravel-kabsa
```

## Examples

Code examples available [see: examples](EXAMPLES.md)

## Use

1. Add the `Kabsa` trait to a model.
Expand Down

0 comments on commit f99c13c

Please sign in to comment.