@@ -130,6 +130,7 @@ public function handle()
130
130
$ model ->getConnection ()->getTablePrefix ().$ model ->getTable (),
131
131
$ this ->getAttributes ($ model ),
132
132
$ this ->getRelations ($ model ),
133
+ $ this ->getObservers ($ model ),
133
134
);
134
135
}
135
136
@@ -247,6 +248,34 @@ protected function getRelations($model)
247
248
->values ();
248
249
}
249
250
251
+ /**
252
+ * Get the Observers watching this model.
253
+ * @return Illuminate\Support\Collection
254
+ */
255
+ protected function getObservers ($ model )
256
+ {
257
+ $ listeners = $ this ->getLaravel ()->make ('events ' )->getRawListeners ();
258
+
259
+ // Distill down to Eloquent observers relevant to this model.
260
+ $ listeners = array_filter ($ listeners , function ($ v , $ key ) use ($ model ) {
261
+ return Str::startsWith ($ key , 'eloquent. ' ) && Str::endsWith ($ key , $ model ::class);
262
+ }, ARRAY_FILTER_USE_BOTH );
263
+
264
+ // Format as Eloquent verb => Observer methods
265
+ $ extractVerb = function ($ key ) {
266
+ preg_match ('/eloquent.([a-zA-Z]+)\: / ' , $ key , $ matches );
267
+ return $ matches [1 ] ?? '? ' ;
268
+ };
269
+
270
+ $ formatted = [];
271
+
272
+ foreach ($ listeners as $ key => $ observerMethods ) {
273
+ $ formatted [] = ['event ' => $ extractVerb ($ key ), 'observer ' => $ observerMethods ];
274
+ }
275
+
276
+ return collect ($ formatted );
277
+ }
278
+
250
279
/**
251
280
* Render the model information.
252
281
*
@@ -255,13 +284,14 @@ protected function getRelations($model)
255
284
* @param string $table
256
285
* @param \Illuminate\Support\Collection $attributes
257
286
* @param \Illuminate\Support\Collection $relations
287
+ * @param \Illuminate\Support\Collection $observers
258
288
* @return void
259
289
*/
260
- protected function display ($ class , $ database , $ table , $ attributes , $ relations )
290
+ protected function display ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
261
291
{
262
292
$ this ->option ('json ' )
263
- ? $ this ->displayJson ($ class , $ database , $ table , $ attributes , $ relations )
264
- : $ this ->displayCli ($ class , $ database , $ table , $ attributes , $ relations );
293
+ ? $ this ->displayJson ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
294
+ : $ this ->displayCli ($ class , $ database , $ table , $ attributes , $ relations, $ observers );
265
295
}
266
296
267
297
/**
@@ -272,9 +302,10 @@ protected function display($class, $database, $table, $attributes, $relations)
272
302
* @param string $table
273
303
* @param \Illuminate\Support\Collection $attributes
274
304
* @param \Illuminate\Support\Collection $relations
305
+ * @param \Illuminate\Support\Collection $observers
275
306
* @return void
276
307
*/
277
- protected function displayJson ($ class , $ database , $ table , $ attributes , $ relations )
308
+ protected function displayJson ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
278
309
{
279
310
$ this ->output ->writeln (
280
311
collect ([
@@ -283,6 +314,7 @@ protected function displayJson($class, $database, $table, $attributes, $relation
283
314
'table ' => $ table ,
284
315
'attributes ' => $ attributes ,
285
316
'relations ' => $ relations ,
317
+ 'observers ' => $ observers ,
286
318
])->toJson ()
287
319
);
288
320
}
@@ -295,9 +327,10 @@ protected function displayJson($class, $database, $table, $attributes, $relation
295
327
* @param string $table
296
328
* @param \Illuminate\Support\Collection $attributes
297
329
* @param \Illuminate\Support\Collection $relations
330
+ * @param \Illuminate\Support\Collection $observers
298
331
* @return void
299
332
*/
300
- protected function displayCli ($ class , $ database , $ table , $ attributes , $ relations )
333
+ protected function displayCli ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
301
334
{
302
335
$ this ->newLine ();
303
336
@@ -339,6 +372,15 @@ protected function displayCli($class, $database, $table, $attributes, $relations
339
372
340
373
$ this ->newLine ();
341
374
375
+ $ this ->components ->twoColumnDetail ('<fg=green;options=bold>Eloquent Observers</> ' );
376
+
377
+ if ($ observers ->count ()) foreach ($ observers as $ observer ) {
378
+ $ this ->components ->twoColumnDetail (
379
+ sprintf ('%s ' , $ observer ['event ' ]), implode (', ' , $ observer ['observer ' ]));
380
+ }
381
+
382
+ $ this ->newLine ();
383
+
342
384
$ this ->components ->twoColumnDetail ('<fg=green;options=bold>Relations</> ' );
343
385
344
386
foreach ($ relations as $ relation ) {
0 commit comments