0

I have the following function written in php\Laravel

public static function getModelByTablename($tableName) {
    $model = new ('App\Models\\'.Str::studly(strtolower(Str::singular($tableName))));
    return $model;
}

image

it works fine on my computer(windows 10) but on the server it doesn't work and give me the following error

syntax error, unexpected '('

  • Does this answer your question? [PHP parse/syntax errors; and how to solve them](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Alon Eitan May 28 '22 at 14:29
  • No, I've read it but it does not work. any other solutions – blue arrow May 28 '22 at 14:31
  • Let's see your entire file. Are you sure included all the necessary classes with the `use` directive for this line of code? – Udo E. May 28 '22 at 14:45

1 Answers1

0

My approach would be this: (not tested)

public static function getModelByTablename($tableName) {
    $model = "App\Models\\" . str($tableName)->singular()->studly();
    return new $model();
}

(str function is only available on laravel 9, otherwise use Str::of with the correct import)

Laisender
  • 714
  • 1
  • 5
  • 10