44use Illuminate \Foundation \AliasLoader ;
55use Illuminate \Support \Str ;
66use Illuminate \Foundation \Application ;
7+ use Illuminate \Database \Eloquent \Model as EloquentModel ;
78
89class UnderstandLaravel5ServiceProvider extends ServiceProvider
910{
@@ -213,7 +214,18 @@ protected function registerModelEventListenerProvider()
213214 return new ModelEventListener ($ logger , $ additional );
214215 });
215216 }
216-
217+
218+ /**
219+ * Detect Laravel version
220+ *
221+ * @param array $versions
222+ * @return type
223+ */
224+ protected function detectLaravelVersion (array $ versions )
225+ {
226+ return Str::startsWith (Application::VERSION , $ versions );
227+ }
228+
217229 /**
218230 * Listen Laravel logs
219231 *
@@ -222,7 +234,7 @@ protected function registerModelEventListenerProvider()
222234 protected function listenLaravelEvents ()
223235 {
224236 // only Laravel versions below L5.4 supports `illuminate.log`
225- if (Str:: startsWith (Application:: VERSION , ['5.0 ' , '5.1 ' , '5.2 ' , '5.3 ' ]))
237+ if ($ this -> detectLaravelVersion ( ['5.0 ' , '5.1 ' , '5.2 ' , '5.3 ' ]))
226238 {
227239 $ this ->app ['events ' ]->listen ('illuminate.log ' , function ($ level , $ message , $ context )
228240 {
@@ -303,10 +315,25 @@ protected function listenEloquentEvents()
303315
304316 foreach ($ events as $ listenerName => $ eventName )
305317 {
306- $ this ->app [ ' events ' ]-> listen ( $ listenerName , function ( $ model ) use ( $ modelLogger , $ eventName )
318+ if ( $ this ->detectLaravelVersion ([ ' 5.0 ' , ' 5.1 ' , ' 5.2 ' , ' 5.3 ' ]) )
307319 {
308- $ modelLogger ->logModelEvent ($ eventName , $ model );
309- });
320+ $ this ->app ['events ' ]->listen ($ listenerName , function ($ model ) use ($ modelLogger , $ eventName )
321+ {
322+ $ modelLevelEventName = 'eloquent. ' . $ eventName . ': ' . get_class ($ model );
323+
324+ $ modelLogger ->logModelEvent ($ eventName , $ model , $ modelLevelEventName );
325+ });
326+ }
327+ else
328+ {
329+ $ this ->app ['events ' ]->listen ($ listenerName , function ($ modelLevelEventName , $ eventPayload ) use ($ modelLogger , $ eventName )
330+ {
331+ if (isset ($ eventPayload [0 ]) && $ eventPayload [0 ] instanceof EloquentModel)
332+ {
333+ $ modelLogger ->logModelEvent ($ eventName , $ eventPayload [0 ], $ modelLevelEventName );
334+ }
335+ });
336+ }
310337 }
311338 }
312339
0 commit comments