|
| 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 | +``` |
0 commit comments