Skip to content

Commit f99c13c

Browse files
committed
Covid 19 for tinkerwell
1 parent 8bb9feb commit f99c13c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

EXAMPLES.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
### Covid 19 Example
3+
4+
```php
5+
class Covid19Date extends \Illuminate\Database\Eloquent\Model
6+
{
7+
use \Awssat\Kabsa\Traits\Kabsa;
8+
9+
}
10+
11+
12+
class Covid19 extends \Illuminate\Database\Eloquent\Model
13+
{
14+
use \Awssat\Kabsa\Traits\Kabsa, \Awssat\Kabsa\Traits\KabsaRelationships;
15+
16+
protected static $total = 0;
17+
18+
public function getRows()
19+
{
20+
return collect(Http::get('https://pomber.github.io/covid19/timeseries.json')
21+
->json())
22+
->map(function ($dates, $country) {
23+
24+
foreach($dates as $date) {
25+
$date['country'] = $country;
26+
Covid19Date::addRow($date);
27+
}
28+
29+
static::$total += end($dates)['confirmed'] ?? 0;
30+
31+
return ['name' => $country];
32+
})->toArray();
33+
}
34+
35+
public function timeline()
36+
{
37+
return $this->hasManyKabsaRows(Covid19Date::class, 'country', 'name');
38+
}
39+
40+
public function getTotalAttribute()
41+
{
42+
return $this->timeline->last()->confirmed;
43+
}
44+
45+
public function yesterday()
46+
{
47+
return $this->timeline->firstWhere('date', now()->subDay()->format('Y-n-d'));
48+
}
49+
50+
public function today()
51+
{
52+
return $this->timeline->firstWhere('date', now()->format('Y-n-d'));
53+
}
54+
55+
public function lastWeek()
56+
{
57+
return $this->timeline->firstWhere('date', now()->subWeek()->format('Y-n-d'));
58+
}
59+
60+
public static function totalAroundTheWorld()
61+
{
62+
return static::$total;
63+
}
64+
}
65+
66+
Covid19::totalAroundTheWorld();
67+
68+
Covid19::firstWhere('name', 'United Kingdom')->lastWeek()->confirmed;
69+
```

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ To get started with Laravel Kabsa, use Composer to add the package to your proje
1818
composer require awssat/laravel-kabsa
1919
```
2020

21+
## Examples
22+
23+
Code examples available [see: examples](EXAMPLES.md)
24+
2125
## Use
2226

2327
1. Add the `Kabsa` trait to a model.

0 commit comments

Comments
 (0)