Last active
June 29, 2025 17:34
-
-
Save calebporzio/cdf70bd390688646fda65490006eb0a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Automatically alias Laravel Model's to their base classname. | |
| * Ex: "App\Models\User" now can just be accessed by "User" | |
| */ | |
| if (! function_exists('aliasModels')) { | |
| function aliasModels() { | |
| $finder = new \Symfony\Component\Finder\Finder(); | |
| $finder->files()->name('*.php')->in(base_path().'/app'); | |
| foreach ($finder as $file) { | |
| $namespace = 'App\\'; | |
| if ($relativePath = $file->getRelativePath()) { | |
| $namespace .= strtr($relativePath, '/', '\\') . '\\'; | |
| } | |
| $class = $namespace . $file->getBasename('.php'); | |
| try { | |
| $r = new \ReflectionClass($class); | |
| if ($r->isSubclassOf('Illuminate\\Database\\Eloquent\\Model')) { | |
| class_alias($class, $file->getBasename('.php')); | |
| } | |
| } catch (Exception $e) { | |
| // | |
| } | |
| } | |
| } | |
| } | |
| aliasModels(); | |
| return [ | |
| 'startupMessage' => '<info>Using local config file (tinker.config.php)</info>', | |
| 'commands' => [ | |
| // new \App\Tinker\TestCommand, | |
| ], | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I couldn't find a way to write simpler functions to include as new commands, unless you write at least an anonymous class - anything under
commandsmust be an instance ofSymfony\Component\Console\Command\Command:If this becomes a pattern on your config file, I guess a simple wrapper function could be created to streamline closures into command instances.