How to get all models in Laravel?

Laravel 6 , Laravel

Nicesnippets

993

07-12-2019


Hi Guys,

In this tutorial, I will learn you give to get all models in laravel.you can simply get all models in laravel.

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"

]

It will help you...


Recommended Posts