How to get all models in Laravel?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
How to get all models in Laravel

Hello dev’s
Today now in this articles, I will show you a example how to get all models in laravel application. We can simply get all models in laravel application.

Example :-

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
    $path = app_path();

    function getModels($path){
        $out = [];
        $results = \File::files($path);
            foreach ($results as $result) {
                if ($result === '.' or $result === '..') continue;
                    $filename = $path . '/' . $result;
                    if (is_dir($filename)) {
                        $out = array_merge($out, getModels($filename));
                    }else{
                        $out[] = substr($filename,0,-4);
                    }
                }
                return $out;
            }
        dd(getModels($path));

        return view('img');
}

Output :-

array:7 [?
  0 => "/var/www/blog/app//var/www/blog/app/CurrentAffair"
  1 => "/var/www/blog/app//var/www/blog/app/Demo"
  2 => "/var/www/blog/app//var/www/blog/app/EventGallery"
  3 => "/var/www/blog/app//var/www/blog/app/Images"
  4 => "/var/www/blog/app//var/www/blog/app/Project"
  5 => "/var/www/blog/app//var/www/blog/app/Test"
  6 => "/var/www/blog/app//var/www/blog/app/User"
]

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us onĀ facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →