Skip to content

Commit 654a214

Browse files
committed
Initial commit
1 parent 523b162 commit 654a214

File tree

11 files changed

+415
-24
lines changed

11 files changed

+415
-24
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.

.github/pull_request_template.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Description
2+
3+
Explain what are you submitting.
4+
5+
### Fixes
6+
7+
Enter the issue you are fixing (e.g. This fixes #1.).
8+
9+
10+
11+

.gitignore

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1 @@
1-
/vendor/
2-
node_modules/
3-
npm-debug.log
4-
yarn-error.log
5-
6-
# Laravel 4 specific
7-
bootstrap/compiled.php
8-
app/storage/
9-
10-
# Laravel 5 & Lumen specific
11-
public/storage
12-
public/hot
13-
14-
# Laravel 5 & Lumen specific with changed public path
15-
public_html/storage
16-
public_html/hot
17-
18-
storage/*.key
19-
.env
20-
Homestead.yaml
21-
Homestead.json
22-
/.vagrant
23-
.phpunit.result.cache
1+
/.idea

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
# laravel-loggable
1+
# Laravel Publishable
2+
3+
Laravel package to log CRUD operations.
4+
5+
6+
## Installation
7+
8+
Add the package to your Laravel app using composer
9+
10+
```
11+
composer require codetech/laravel-loggable
12+
```
13+
14+
15+
### Service Provider
16+
17+
Register the package's service provider in config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.
18+
19+
```
20+
'providers' => [
21+
22+
...
23+
CodeTech\Loggable\Providers\LoggableServiceProvider::class,
24+
...
25+
26+
];
27+
```
28+
29+
30+
### Migrations
31+
32+
Execute the next Artisan command to run the migrations.
33+
34+
```
35+
php artisan migrate
36+
37+
```
38+
39+
40+
## How to use
41+
42+
To start logging CRUD operations simply use the trait on your models.
43+
44+
```
45+
class Post extends Model
46+
{
47+
use Loggable;
48+
49+
...
50+
```

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "codetech/laravel-loggable",
3+
"description": "Laravel package for handling publishable resources.",
4+
"keywords": [
5+
"codetech",
6+
"laravel-loggable",
7+
"laravel",
8+
"crud",
9+
"logging"
10+
],
11+
"authors": [
12+
{
13+
"name": "José Osório",
14+
"email": "jfrosorio@gmail.com",
15+
"role": "Developer"
16+
}
17+
],
18+
"license": "MIT",
19+
"require": {
20+
"php": "^7.2",
21+
"laravel/framework": "~6.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"CodeTech\\Loggable\\": "src/"
26+
}
27+
},
28+
"minimum-stability": "dev",
29+
"prefer-stable": true,
30+
"extra": {
31+
"laravel": {
32+
"providers": [
33+
"CodeTech\\Loggable\\Providers\\LoggableServiceProvider"
34+
]
35+
}
36+
}
37+
}

src/ModelLog.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace CodeTech\Loggable\ModelLog;
4+
5+
use CodeTechCMS\Modules\Users\Models\User;
6+
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
class ModelLog extends Model
10+
{
11+
protected $fillable = [
12+
'loggable_id',
13+
'loggable_type',
14+
'operation',
15+
'user_id'
16+
];
17+
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Scopes
22+
|--------------------------------------------------------------------------
23+
*/
24+
25+
/**
26+
* Scope a query to only include created operations.
27+
*
28+
* @param Builder $query
29+
* @return Builder
30+
*/
31+
public function scopeCreated(Builder $query)
32+
{
33+
return $query->where('operation', 'created');
34+
}
35+
36+
/**
37+
* Scope a query to only include updated operations.
38+
*
39+
* @param Builder $query
40+
* @return Builder
41+
*/
42+
public function scopeUpdated(Builder $query)
43+
{
44+
return $query->where('operation', 'updated');
45+
}
46+
47+
/**
48+
* Scope a query to only include deleted operations.
49+
*
50+
* @param Builder $query
51+
* @return Builder
52+
*/
53+
public function scopeDeleted(Builder $query)
54+
{
55+
return $query->where('operation', 'deleted');
56+
}
57+
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Relations
62+
|--------------------------------------------------------------------------
63+
*/
64+
65+
/**
66+
* Get the owning log model.
67+
*/
68+
public function loggable()
69+
{
70+
return $this->morphTo();
71+
}
72+
73+
/**
74+
* Get the user that owns this model log.
75+
*
76+
* @return User
77+
*/
78+
public function user()
79+
{
80+
return $this->belongsTo(User::class);
81+
}
82+
}

src/Observers/LoggableObserver.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace CodeTech\Loggable\Observers;
4+
5+
use CodeTech\Loggable\ModelLog\ModelLog;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class LoggableObserver
9+
{
10+
/**
11+
* Listen to the created event.
12+
*
13+
* @param Model $model
14+
*/
15+
public function created(Model $model): void
16+
{
17+
$this->saveLog($model, 'created');
18+
}
19+
20+
/**
21+
* Listen to the updated event.
22+
*
23+
* @param Model $model
24+
*/
25+
public function updated(Model $model): void
26+
{
27+
$this->saveLog($model, 'updated');
28+
}
29+
30+
/**
31+
* Listen to the deleted event.
32+
*
33+
* @param Model $model
34+
*/
35+
public function deleted(Model $model): void
36+
{
37+
$this->saveLog($model, 'deleted');
38+
}
39+
40+
41+
/**
42+
* Saves the action on the database.
43+
*
44+
* @param Model $model
45+
* @param string $action
46+
*/
47+
private function saveLog(Model $model, string $action): void
48+
{
49+
ModelLog::create(
50+
[
51+
'loggable_id' => $model->id,
52+
'loggable_type' => get_class($model),
53+
'operation' => $action,
54+
'user_id' => auth()->check() ? auth()->id() : null
55+
]
56+
);
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CodeTech\Loggable\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LoggableServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
// Load migrations from custom path
27+
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
28+
}
29+
}

0 commit comments

Comments
 (0)