能否通过字符串调用Laravel模型?
这就是我想要实现的,但却失败了:
$model_name = 'User';
$model_name::where('id', $id)->first();
我遇到了以下异常:
exception 'ErrorException' with message 'Undefined variable: User'
能否通过字符串调用Laravel模型?
这就是我想要实现的,但却失败了:
$model_name = 'User';
$model_name::where('id', $id)->first();
我遇到了以下异常:
exception 'ErrorException' with message 'Undefined variable: User'
是的,你可以这样做,但需要使用完全限定的类名:
$model_name = 'App\Model\User';
$model_name::where('id', $id)->first();
如果您的模型名称存储在除普通变量(例如对象属性)以外的其他地方,您需要使用中间变量才能使其正常工作。
$model = $this->model_name;
$model::where('id', $id)->first();
试一试:
$model_name = 'User';
$model = app("App\Model\{$model_name}");
$model->where('id', $id)->first();
$modelPath = "App\Models\\{$modelName}";
- Luke这种方法应该很有效:
最初的回答
private function getNamespace($model){
$dirs = glob('../app/Models/*/*');
return array_map(function ($dir) use ($model) {
if (strpos($dir, $model)) {
return ucfirst(str_replace(
'/',
'\\',
str_replace(['../', '.php'], '', $dir)
));
}
}, array_filter($dirs, function ($dir) use ($model) {
return strpos($dir, $model);
}));
}
$model = current($this->getNamespace($this->request->get('model')));
恢复模型的命名空间,然后只需调用我的查询:$model::all()
。"最初的回答"function convertVariableToModelName($modelName='',$nameSpace='App')
{
if (empty($nameSpace) || is_null($nameSpace) || $nameSpace === "")
{
$modelNameWithNameSpace = "App".'\\'.$modelName;
return app($modelNameWithNameSpace);
}
if (is_array($nameSpace))
{
$nameSpace = implode('\\', $nameSpace);
$modelNameWithNameSpace = $nameSpace.'\\'.$modelName;
return app($modelNameWithNameSpace);
}elseif (!is_array($nameSpace))
{
$modelNameWithNameSpace = $nameSpace.'\\'.$modelName;
return app($modelNameWithNameSpace);
}
}
例如,如果您想获取所有用户
情景1:
$userModel= convertVariableToModelName('User');
$result = $userModel::all();
情境2:
如果您的模型在自定义命名空间中,可能是App\Models
$userModel= convertVariableToModelName('User',['App','Models']);
$result = $userModel::all();
希望能有所帮助
是的,你可以用更灵活的方式来实现。创建一个通用函数。
function getWhere($yourModel, $select, $is_model_full_path=null, $condition_arr=null)
{
if(!$is_model_full_path){ // if your model exist in App directory
$yourModel ="App\\$yourModel";
}
if(!$condition_arr){
return $yourModel::all($select)->toArray();
}
return $App_models_yourModel::all($select)->where($condition_arr)->toArray();
}
现在你可以以不同的方式调用这个方法。
getWhere('User', ['id', 'name']); // with default path of model
getWhere('App\Models\User', ['id', 'name'], true); // with custom path of model
getWhere('User', ['id', 'name'], false, ['id', 1]); // with condition array
顺便说一下,我喜欢使用这样的函数。
你也可以尝试这样做:
use App\Model\User;
class UsersController extends Controller
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function getUser()
{
$data = $this->model->where('id', $id)->first();
return $data;
}
}
$model_name = 'App\Http\Model\User';
。 - Cerlin${$this->model_name}::where
- Parampal Pooniapp($model_name)->where(...)
。 - Yoram de Langen