Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get a list of all models Laravel

// oneliner
dd(collect(glob(app_path('Models') . '/*.php'))->map(fn ($file) => basename($file, '.php'))->toArray());
Comment

laravel list of models

<?php
// found link from https://stackoverflow.com/questions/34053585/how-do-i-get-a-list-of-all-models-in-laravel
// and then finally found on https://gist.github.com/mohammad425/231242958edb640601108bdea7bcf9ac
function getAllModels(): array
{
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
        $models = [];
        foreach ((array)data_get($composer, 'autoload.psr-4') as $namespace => $path) {
            $models = array_merge(collect(File::allFiles(base_path($path)))
                ->map(function ($item) use ($namespace) {
                    $path = $item->getRelativePathName();
                    return sprintf('\%s%s',
                        $namespace,
                        strtr(substr($path, 0, strrpos($path, '.')), '/', ''));
                })
                ->filter(function ($class) {
                    $valid = false;
                    if (class_exists($class)) {
                        $reflection = new ReflectionClass($class);
                        $valid = $reflection->isSubclassOf(IlluminateDatabaseEloquentModel::class) &&
                            !$reflection->isAbstract();
                    }
                    return $valid;
                })
                ->values()
                ->toArray(), $models);
        }
        return $models;
}
Comment

PREVIOUS NEXT
Code Example
Php :: what is abstract class in php 
Php :: laravel 8 add column to existing table 
Php :: php all date arguments 
Php :: $_SESSION php example 
Php :: IlluminateContractsAuthAuthenticatable, AppModelsUser given, called in 
Php :: if user name is wordpress 
Php :: - in php 
Php :: db seed in controller 
Php :: laravel model soft delete 
Php :: php string functions 
Php :: text box should accept only alphanumeric not special characters in php 
Php :: Multiple image upload with CodeIgniter 
Php :: php check if valid xml 
Php :: how to get the root domain in laravel 
Php :: how run job laravel in cpanel host 
Php :: violation: 1071 Specified key was too long; max key length is 1000 bytes 
Php :: contact form 7 remove br 
Php :: Woocommerce get image galleries by product id 
Php :: php multiplication 
Php :: php if time is greater than 
Php :: array_merge 
Php :: merge array in php 
Php :: php if input is empty 
Php :: enable extensions in php.ini 
Php :: php flatten array 
Php :: apache use public folder as root 
Php :: wc get product category image 
Php :: php get html tags from string 
Php :: Calling itself a static function in php 
Php :: laravel get namespace 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =