File tree 3 files changed +51
-0
lines changed
3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ .DS_Store
2
+ .idea
3
+ /vendor
Original file line number Diff line number Diff line change
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
+
10
+ }
11
+ ],
12
+ "autoload" : {
13
+ "psr-4" : {
14
+ "BinaryCabin\\ LaravelAuthor\\ " : " src/"
15
+ }
16
+ }
17
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments