Skip to content

Commit c13cfd1

Browse files
committed
init
0 parents  commit c13cfd1

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
.idea
3+
/vendor

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "binarycabin/laravel-author",
3+
"description": "A simple way to generate the model's author user when created",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Jeff Kilroy",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"BinaryCabin\\LaravelAuthor\\": "src/"
15+
}
16+
}
17+
}

src/Traits/HasAuthorUser.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace BinaryCabin\LaravelAuthor\Traits;
4+
5+
trait HasAuthorUser{
6+
7+
public static function bootHasAuthorUser(){
8+
static::creating(function($model) {
9+
$authorUserIdFieldName = $model->getAuthorUserIdFieldName();
10+
if(empty($model->$authorUserIdFieldName) && \Auth::user()){
11+
$model->$authorUserIdFieldName = \Auth::user()->id;
12+
}
13+
});
14+
}
15+
16+
public function getAuthorUserIdFieldName(){
17+
if(!empty($this->authorUserIdFieldName)){
18+
return $this->authorUserIdFieldName;
19+
}
20+
return 'author_user_id';
21+
}
22+
23+
public function authorUser(){
24+
return $this->belongsTo(\App\User::class,$this->getAuthorUserIdFieldName());
25+
}
26+
27+
public function scopeByAuthorUser($query, $authorUserId){
28+
return $query->where($this->getAuthorUserIdFieldName(),$authorUserId);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)