How do I get a list of all models in Laravel?
Determining every model and its corresponding database table within a Laravel application is a common requirement when auditing data integrity or building analytical dashboards. While Laravel does not provide a single built-in method to dump the entire class hierarchy automatically, you can derive this information reliably by inspecting the migration history or querying the meta-data stored in the framework's core tables.
The most robust approach for building a dashboard that tracks data changes over timeâsuch as counting user additions or modifications per dayâis to leverage the migrations table. Every Laravel project maintains a record of its schema evolution here. You can iterate through this table, extract the table column name, and map it directly to the model class responsible for managing that dataset.
To implement this, you can run a simple query against the database. Since the migrations table is part of the core Laravel framework and exists in almost every project, it serves as a definitive source of truth regardless of how your models are named or organized in the app/Models directory.
// Example: Retrieving all tables from the migrations history
$tables = DB::table('migrations')
->select('table')
->distinct()
->get();
However, mapping these table names back to their specific Model classes requires a bit more logic. In Laravel 10 and later, models are often located in the app/Models namespace or the root app directory. A practical strategy is to scan the filesystem for files ending in .php that define Eloquent models. You can iterate through the app/Models directory (and potentially the root app folder for legacy projects) and use reflection or a simple string check to identify classes extending Illuminate\Database\Eloquent\Model.
Here is a conceptual approach to building a mapping array:
$models = [];
// Scan the Models directory
if (file_exists(app_path('Models'))) {
$files = glob(app_path('Models') . '/*.php');
foreach ($files as $file) {
$content = file_get_contents($file);
// Look for class definitions that extend Model
if (preg_match('/class (\w+) extends\s+Illuminate\\\\Database\\\\Eloquent\\\\Model/', $content, $matches)) {
$modelName = $matches[1];
// Determine the table name associated with this model
$reflection = new ReflectionClass($modelName);
$table = $reflection->getMethod('getConnection')->invoke(new $modelName)->getTable();
// Alternatively, if using dynamic tables or overridden methods,
// you might need to inspect the 'table' property directly via reflection.
}
}
}
Trade-offs and Limitations
Relying solely on the migrations table gives you a list of tables but not necessarily the class names unless you cross-reference with the file system. This is often the safer bet because models can be renamed or moved without altering migration history, provided they still use the correct table name. Conversely, scanning the file system might miss dynamically registered models if your application relies heavily on service providers to register classes in the $app['events']->listen array or uses traits that obscure the primary class definition.
For your specific use caseâbuilding a dashboard to visualize data changesâthe critical insight is that you don't necessarily need the PHP class names to perform aggregation queries. You primarily need the table names. Once you have the list of tables from the migrations table, you can construct generic queries or iterate through them to count rows based on timestamps.
If you require specific model behavior (like checking for custom casts or accessors), you must ensure your scanning logic includes those classes. A common pitfall is assuming that a file named User.php maps strictly to the users table; Laravel supports dynamic table names via the $table property, which breaks simple filename-to-table assumptions. Always verify the actual table name via reflection or the model's configuration before building your analytics logic.
For further resources on managing database metadata and migrations in Laravel, you might explore https://laravelcompany.com to see how other developers handle similar architectural challenges.