Skip to content

Commit 5e2175e

Browse files
committed
fix
1 parent 893cf4d commit 5e2175e

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,88 @@
1-
# laravel-model-view-counter
1+
# Model View Counter
2+
3+
Model View Counter, Laravel modellerinizin görüntülenme sayılarını takip etmenizi sağlayan ve cache yapısı kullanarak performansı optimize eden bir pakettir. Bu paket sayesinde, herhangi bir modelin kaç kez görüntülendiğini kolayca takip edebilir ve analiz edebilirsiniz.
4+
5+
## Özellikler
6+
7+
- **Model Bazlı Görüntülenme Sayacı**: Herhangi bir Laravel modelinin görüntülenme sayısını takip edin.
8+
- **Cache Desteği**: Görüntülenme sayıları cache’de tutularak performans artırılır.
9+
- **Kolay Entegrasyon**: Modellerinize basit bir trait ekleyerek hızlıca kullanmaya başlayın.
10+
- **Event ve Listener**: Laravel’in event sistemi kullanılarak esnek bir yapı sunulur.
11+
- **Cache Temizleme Komutu**: Cache’i temizlemek için Artisan komutu içerir.
12+
13+
## Kurulum
14+
15+
```json
16+
"repositories": [
17+
{
18+
"type": "vcs",
19+
"url": "https://github.com/DigitalCoreHub/laravel-model-view-counter.git"
20+
}
21+
],
22+
```
23+
24+
```bash
25+
composer require digitalcorehub/laravel-model-view-counter
26+
php artisan vendor:publish
27+
php artisan migrate
28+
```
29+
30+
## Yapılandırma
31+
32+
`config/model-view-counter.php` dosyasında paketinizin ayarlarını yapılandırabilirsiniz:
33+
34+
```php
35+
return [
36+
'models' => [
37+
/*
38+
Örnek:
39+
App\Models\User::class,
40+
App\Models\Blog::class,
41+
*/
42+
],
43+
'cache_enabled' => true, // Cache özelliğini etkinleştirmek için
44+
'cache_threshold' => 10, // Cache'de birikmesi gereken minimum sayı
45+
'cache_key' => 'model_view_counts', // Cache anahtarı
46+
];
47+
```
48+
49+
Kullanmak istediğiniz modelleri tanımlamayı unutmayın.
50+
51+
## Kullanım
52+
53+
### Modellerinize Trait’i Ekleyin
54+
55+
```php
56+
namespace App\Models;
57+
58+
use Illuminate\Foundation\Auth\User as Authenticatable;
59+
use DigitalCoreHub\LaravelModelViewCounter\Traits\CountableView;
60+
61+
class User extends Authenticatable
62+
{
63+
use CountableView;
64+
}
65+
```
66+
67+
### Tetikleyin
68+
69+
```php
70+
// Modeliniz görüntülendiğinde ModelViewed event’ini tetikleyin.
71+
Route::get('users/{user:id}', function (User $user) {
72+
event(new ModelViewed($user));
73+
});
74+
```
75+
76+
### Görüntülenme Sayısını Çekin
77+
78+
```html
79+
<!-- Görüntülenme sayısını çekin -->
80+
<h1>{{ $user->name }}</h1>
81+
<p>Görüntülenme Sayısı: {{ $user->viewCount() }}</p>
82+
```
83+
84+
### Görüntülenme Sayısını Arttırın
85+
86+
```php
87+
$user->incrementViewCount();
88+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "digitalcorehub/laravel-model-view-counter",
3-
"description": "//",
3+
"description": "A Laravel package to count model views with caching support.",
44
"type": "library",
55
"license": "MIT",
66
"autoload": {

config/model-view-counter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
return [
44
'models' => [
55
/*
6-
Example:
7-
App\Models\Post::class,
8-
App\Models\Blogs::class,
6+
* Example Models
7+
* App\Models\Post::class,
8+
* App\Models\Blogs::class,
9+
* App\Models\User::class,
910
*/
1011
],
11-
'cache_enabled' => true, // To enable caching feature
12+
'cache_enabled' => false, // To enable caching feature
1213
'cache_threshold' => 10, // Minimum count to accumulate in cache
1314
'cache_key' => 'model_view_counts', // Cache key
1415
];

src/Traits/CountableView.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,39 @@ protected function incrementViewCountWithCache()
2424

2525
$modelKey = $this->getCacheModelKey();
2626

27-
// Önceki sayımları al
27+
/*
28+
* Önceki sayımları al
29+
* Get previous counts
30+
*/
2831
$counts = Cache::get($cacheKey, []);
2932

30-
// Modelin sayımını artır
33+
/*
34+
* Modelin sayımını artır
35+
* Increase the number of models
36+
*/
3137
if (isset($counts[$modelKey])) {
3238
$counts[$modelKey]++;
3339
} else {
3440
$counts[$modelKey] = 1;
3541
}
3642

37-
// Güncellenmiş sayımları önbelleğe kaydet
43+
/*
44+
* Güncellenmiş sayımları önbelleğe kaydet
45+
* Cache updated counts
46+
*/
3847
Cache::put($cacheKey, $counts);
3948

40-
// Eğer eşik değeri aşıldıysa veritabanına kaydet
49+
/*
50+
* Eğer eşik değeri aşıldıysa veritabanına kaydet
51+
* If threshold value is exceeded, save to database
52+
*/
4153
if ($counts[$modelKey] >= $threshold) {
4254
$this->persistCountsToDatabase($modelKey, $counts[$modelKey]);
4355

44-
// Sayımı sıfırla
56+
/*
57+
* Sayımı sıfırla
58+
* Reset count
59+
*/
4560
$counts[$modelKey] = 0;
4661
Cache::put($cacheKey, $counts);
4762
}

0 commit comments

Comments
 (0)