当模型名称存储在变量中时,有没有办法通过变量动态访问laravel模型?

Basically what I need to do is:

$x = 'Admin';
$model = new \ReflectionClass($x);
$model->getFieldList();

Where I have Admin model inside app folder. Obviously, this doesn't work. Does anyone have any idea? Can it be done?

you can do this, but you need to use the fully qualified class name.for example My models are in the Model directory :

$model = 'App\Model\User';
$user=$model::where('id', $id)->first();

Firstly you need to add the namespace to your model. By default this is App\. So your string would have to be "\App\Admin". Now you can simply create a class instance using this string.

$x = '\\App\\Admin';
$model = new $x();