Inside this article we will see the concept i.e Laravel 9 How To get all models from application. Article contains the classified information about getting laravel application models. This concept is very useful when you want to list all application models of laravel application.
Article considers that you stored all models into /app/Models folder. We will use PHP function scandir and the concept of recursive function here.
Learn More –
- Laravel 9 Get All Dates Between Two Dates Using Carbon
- PHP How To Work with Cookie Set Get And Delete Tutorial
- Laravel 9 Daily Weekly Monthly Automatic Database Backup
- PHP How To Work with Data Pagination Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Consider Laravel Models (In Application)
Here,
Consider these models into folder /app/Models.
Now, To list all application models from application we will a very concept.
List All Laravel Models
Open a controller and write this code into it.
<?php namespace App\Http\Controllers; class CustomController extends Controller { /** * Write code on Method * * @return response() */ public function index() { $allModels = $this->getAllModels(); echo "<pre>"; print_r($allModels); } /** * Write code on Method * * @return response() */ public function getAllModels($sub_folder = "") { $modelList = []; if (!empty($sub_folder)) { $path = app_path() . "/Models/" . $sub_folder; } else { $path = app_path() . "/Models"; } $results = scandir($path); foreach ($results as $result) { if ($result === '.' || $result === '..') continue; $filename = $result; if (is_dir($path . "/" . $filename)) { $modelList = array_merge($modelList, $this->getAllModels($filename)); } else { $modelList[] = substr($filename, 0, -4); } } return $modelList; } }
getAllModels() This method lists all models from application. If /Models folder contain any sub folder then the same method will call as a recursive function to list inner files.
Concept
public function getAllModels($sub_folder = "")
{
$modelList = [];
if (!empty($sub_folder)) {
$path = app_path() . "/Models/" . $sub_folder;
} else {
$path = app_path() . "/Models";
}
$results = scandir($path);
foreach ($results as $result) {
if ($result === '.' || $result === '..') continue;
$filename = $result;
if (is_dir($path . "/" . $filename)) {
$modelList = array_merge($modelList, $this->getAllModels($filename));
} else {
$modelList[] = substr($filename, 0, -4);
}
}
return $modelList;
}
Output
We hope this article helped you to learn Laravel 9 How To Get All Models From Application Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.