List All the Models of the Laravel Application

Hello Artisans, today I'll show you how you can list all of the models in your Laravel Application. Sometimes we need to see the list of models for any kind of client requirements. But we know that Laravel doesn't provide any such methods. But we'll know that Laravel keep all of the models under app\Models, so using that we can easily retrieve all the models name. So let's see how we can retrieve the list of models in our Laravel application.

Note: Tested on Laravel 9.19.

List All the Models

As we already know as well as earlier said that we can retrieve all the model list by scanning the app/Models directory. So, let's see below code snippet how we'll retrieve the list.

public function index()
        $modelList = [];
        $path = app_path() . "/Models";
        $results = scandir($path);

        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;
            $filename = $result;
            $modelList[] = substr($filename,0,-4);


Here, what we actually do is, first we'll scan the folder path of app/Models as we know that Laravel keeps all the models in that directory. Then from each file name remove the the last 4 character, as we need to get the name of just Model name without php extension. Like if we've model name User.php, we'll print only User, that's why we'll substr() the last 4 character. And after that we'll dd() the model List. And if everything is fine (hope so) It'll print the below output.

Model List

That's it for today which is a small tip for you. I hope you've enjoyed this tutorial as well as it can be helpful for you. If you face any difficulties, let me know through the comment box. Thanks for reading. ๐Ÿ™‚