有没有办法通过表名获取模型?
例如,我有一个“用户”模型,其表定义为protected $table = "users"
现在,我想做的是通过等于“users”的表名获取模型。
这个函数更像是相反的Model::getTable();
我到处搜索但找不到解决方案,也许我可能错过了一些简单的东西?
EDIT
我正在构建类似 API 的东西:
Route::get('/{table}', 'ApiController@api');
Route::get('/{table}/filter', 'ApiController@filter');
Route::get('/{table}/sort', 'ApiController@sort');
Route::get('/{table}/search', 'ApiController@search');
因此,在地址栏中,例如当我搜索“用户”时,我可以直接点击 URL:
api/users/search?id=1
然后在控制器上,类似:
public function search(){
// get all the params
// get the model function
$model = //function to get model by table name
// do some filtering, then return the model
return $model;
}
也许这样的事情会对你有所帮助:
$className = 'App\\' . studly_case(str_singular($tableName));
if(class_exists($className)) {
$model = new $className;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)