jeudi 14 décembre 2017

How to get a list of all Eloquent Models in Laravel using reflection?

I am hoping to find a way to get a list of all Eloquent models in my app. Perhaps I need to use reflection.

Here is what I tried to do. However, this does not work because get_declared_classes()

  1. returns an array of strings not objects.
  2. does not show the model classes.

.

<?php
namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class IdentifierGeneratorServiceProvider extends ServiceProvider
{
    public function boot()
    {
        foreach (get_declared_classes() as $class) {
            if (!($class instanceof Model)) {
                continue;
            }
            // At this point I know that $class is an instance of Illuminate\Database\Eloquent\Model
            // I should be able to call any method of the Model object
            // like this $class->getIncrementing(); 
        }
    }

    public function register()
    {

    }
}





Aucun commentaire:

Enregistrer un commentaire