Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit 6000b5c

Browse files
committed
added Statistic model and updated readme
1 parent d00b3c4 commit 6000b5c

3 files changed

Lines changed: 71 additions & 22 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,40 @@ IN
3737
('articles', 'projects', 'tasks')
3838
```
3939

40+
Or, better still, use the Eloquent model to query the data:
41+
42+
```php
43+
use Statistics\Models\Statistic;
44+
45+
$stats = Statistic::query()
46+
->whereIn('table', ['articles', 'projects', 'tasks'])
47+
->get(['table', 'values']);
48+
```
49+
4050
| Table | Values |
4151
| --------- | ----------------- |
4252
| Articles | `{ "count" : 6 }` |
4353
| Projects | `{ "count" : 3 }` |
4454
| Tasks | `{ "count" : 2 }` |
4555

56+
This becomes even more powerful when using joins for individual rows:
57+
58+
```php
59+
$users = User::query()
60+
->join('statistics', function ($join) {
61+
$join->on('users.id', '=', 'statistics.id')
62+
->where('statistics.table', 'users');
63+
})
64+
->get(['users.name', DB::raw('`values`->"$.post_count" AS `posts`')])
65+
->orderByRaw('`values`->"$.post_count" DESC')
66+
```
67+
68+
| Name | Posts |
69+
| ----- | ----- |
70+
| John | 6 |
71+
| Fred | 4 |
72+
| Dave | 1 |
73+
4674
## How does it work?
4775

4876
The package will automatically register and migrate a `statistics` table to your database. This table then serves as a repository for aggregated values. You can then easily join records to this table to get the associated metrics you need.

src/Models/Statistic.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Statistics\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Statistic extends Model
8+
{
9+
protected $guarded = [];
10+
11+
/**
12+
* Get the table associated with the model.
13+
*
14+
*/
15+
public function getTable() : string
16+
{
17+
return config('statistics.table', 'statistics');
18+
}
19+
}

tests/Test.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Statistics\Tests;
44

5+
use Statistics\Models\Statistic;
56
use Orchestra\Testbench\TestCase;
6-
use Illuminate\Support\Facades\DB;
7+
use Statistics\Tests\Models\Post;
8+
use Statistics\Tests\Models\User;
79
use Statistics\Tests\World\Builder;
810

911
class Test extends TestCase
@@ -21,51 +23,51 @@ protected function setUp() : void
2123
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
2224
$this->loadMigrationsFrom(__DIR__ . '/Migrations');
2325

24-
DB::table('users')->truncate();
25-
DB::table('posts')->truncate();
26+
User::truncate();
27+
Post::truncate();
2628
}
2729

2830
/** @test */
2931
public function it_creates_triggers_for_inserted_and_deleted_user_records() : void
3032
{
31-
$this->assertCount(0, DB::table('statistics')->get());
33+
$this->assertCount(0, Statistic::get());
3234

33-
DB::table('users')->insert(['id' => 1, 'name' => 'John Doe']);
35+
User::insert(['id' => 1, 'name' => 'John Doe']);
3436

35-
$this->assertCount(2, DB::table('statistics')->get());
37+
$this->assertCount(2, Statistic::get());
3638

37-
$statistics = DB::table('statistics')
39+
$statistics = Statistic::query()
3840
->orderBy('table')
3941
->orderBy('id')
4042
->get();
4143

42-
$this->assertEquals('', $statistics[0]->id);
44+
$this->assertEquals('', $statistics[0]->getRawOriginal('id'));
4345
$this->assertEquals('users', $statistics[0]->table);
4446
$this->assertEquals(['count' => 1], json_decode($statistics[0]->values, true));
4547

46-
$this->assertEquals('1', $statistics[1]->id);
48+
$this->assertEquals('1', $statistics[1]->getRawOriginal('id'));
4749
$this->assertEquals('users', $statistics[1]->table);
4850
$this->assertEquals(['post_sum_likes' => '', 'post_count' => ''], json_decode($statistics[1]->values, true));
4951

50-
DB::table('users')->delete();
52+
User::query()->delete();
5153

52-
$this->assertCount(1, DB::table('statistics')->get());
54+
$this->assertCount(1, Statistic::get());
5355

54-
$this->assertEquals('', DB::table('statistics')->first()->id);
55-
$this->assertEquals('users', DB::table('statistics')->first()->table);
56-
$this->assertEquals(['count' => 0], json_decode(DB::table('statistics')->first()->values, true));
56+
$this->assertEquals('', Statistic::first()->getRawOriginal('id'));
57+
$this->assertEquals('users', Statistic::first()->table);
58+
$this->assertEquals(['count' => 0], json_decode(Statistic::first()->values, true));
5759
}
5860

5961
/** @test */
6062
public function it_creates_triggers_and_statistics_for_seeded_records() : void
6163
{
6264
Builder::seed();
6365

64-
$this->assertCount(2, DB::table('users')->get());
65-
$this->assertCount(8, DB::table('posts')->get());
66-
$this->assertCount(4, DB::table('statistics')->get());
66+
$this->assertCount(2, User::get());
67+
$this->assertCount(8, Post::get());
68+
$this->assertCount(4, Statistic::get());
6769

68-
$statistics = DB::table('statistics')
70+
$statistics = Statistic::query()
6971
->orderBy('table')
7072
->orderBy('id')
7173
->get();
@@ -78,19 +80,19 @@ public function it_creates_triggers_and_statistics_for_seeded_records() : void
7880
'likes_sum' => 22,
7981
];
8082

81-
$this->assertEquals('', $statistics[0]->id);
83+
$this->assertEquals('', $statistics[0]->getRawOriginal('id'));
8284
$this->assertEquals('posts', $statistics[0]->table);
8385
$this->assertEquals($expected, json_decode($statistics[0]->values, true));
8486

85-
$this->assertEquals('', $statistics[1]->id);
87+
$this->assertEquals('', $statistics[1]->getRawOriginal('id'));
8688
$this->assertEquals('users', $statistics[1]->table);
8789
$this->assertEquals(['count' => 2], json_decode($statistics[1]->values, true));
8890

89-
$this->assertEquals('1', $statistics[2]->id);
91+
$this->assertEquals('1', $statistics[2]->getRawOriginal('id'));
9092
$this->assertEquals('users', $statistics[2]->table);
9193
$this->assertEquals(['post_sum_likes' => 8, 'post_count' => 4], json_decode($statistics[2]->values, true));
9294

93-
$this->assertEquals('2', $statistics[3]->id);
95+
$this->assertEquals('2', $statistics[3]->getRawOriginal('id'));
9496
$this->assertEquals('users', $statistics[3]->table);
9597
$this->assertEquals(['post_sum_likes' => 14, 'post_count' => 4], json_decode($statistics[3]->values, true));
9698
}

0 commit comments

Comments
 (0)