From f99c13ce7cec1dc7b769183e8e7faa93c5e4cc65 Mon Sep 17 00:00:00 2001 From: Bader Date: Mon, 30 Mar 2020 17:56:40 +0300 Subject: [PATCH] Covid 19 for tinkerwell --- EXAMPLES.md | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 4 ++++ 2 files changed, 73 insertions(+) create mode 100644 EXAMPLES.md diff --git a/EXAMPLES.md b/EXAMPLES.md new file mode 100644 index 0000000..71a9671 --- /dev/null +++ b/EXAMPLES.md @@ -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; +``` \ No newline at end of file diff --git a/README.md b/README.md index 31b44de..ce1bb82 100755 --- a/README.md +++ b/README.md @@ -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.