Skip to content

Instantly share code, notes, and snippets.

@calebporzio
Last active January 24, 2024 08:46
Show Gist options
  • Save calebporzio/cdf70bd390688646fda65490006eb0a6 to your computer and use it in GitHub Desktop.
Save calebporzio/cdf70bd390688646fda65490006eb0a6 to your computer and use it in GitHub Desktop.
<?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,
],
];
@mojtabaahn
Copy link

Thanks man, great article and great gist.

@calebporzio
Copy link
Author

My pleasure!

@carcinocron
Copy link

what about defining custom functions for use in tinker?

@calebporzio
Copy link
Author

Should be as simple as defining a function in the config, but I don't remember off hand. Shouldn't be too difficult though with a little research into tinker config files. (looking particularly at the underlying package: PsySh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment