简体   繁体   中英

(laravel) Query builder using model of variable name

I'm trying to insert into a table using its laravel model class, but I need to be able to insert into the table indicated by a variable. How can this be done? I tried the vanilla PHP moethod and the laravel facade method but both gave me errors:

$modelName = "model";

# this works no problem:
$model = new model();

# this blows up:
$model = new $modelName();

# this blows up:
$model::create($request->all());

the error given by laravel:

FatalThrowableError in Controller.php line 44:
Class 'model' not found

You must put the fully qualified class name:

One must use the fully qualified name (class name with namespace prefix).

$modelName = "App\\User"; // assuming User is located in the App namespace
$model = new $modelName();

You can learn more about it here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM