I added a helper function file in composer.json right after the psr-4 entry
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Http/Helpers/helpers.php"
]
},
but it throws a syntax error:
<?php
namespace App\Http\Helpers;
if (!function_exists('entity_url')) {
function entity_show($entityType, $id, $locale = null) {
if (is_null($locale) && function_exists('app')) {
$locale = app()->getLocale();
}
$entity = \App\Models\{ $entityType }::find($id); // syntax error
}
}
Parse error: syntax error, unexpected token "\"
If I use any existing actual Model it works: $entity = \App\Models\Foobar. I have several cases in my code where the complex curly syntax ({ $entityType }) is no problem, why is it here?
Edit
$class = '\\App\\Models\\' . $entityType; and $entity = "\\App\\Models\\$entityType"::find($id); work, I still dont understand why though. Throughout many controllers I can easily use \App\Models\{$entityType}::.
$class = '\App\Models\' . $entityType; $entity = $class::find($id);