如何在 Laravel 中返回数据库表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14082682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return database table name in Laravel
提问by Chris G
Is there a way that I can get the current database table in use by the model that I'm in? I see that there is a table() function in Laravel/Database/Eloquent/model.php but I've been unsuccessful calling it calling it from the model that I'm in.
有没有办法让我使用的模型正在使用当前的数据库表?我看到 Laravel/Database/Eloquent/model.php 中有一个 table() 函数,但我一直没有成功调用它从我所在的模型中调用它。
采纳答案by Laurence
Edit April 2019: This answer is now out of date. See the new correct answer by Flyn San
2019 年 4 月编辑:此答案现已过时。查看 Flyn San 的新正确答案
Yes - Eloquent has a $table
variable. There are two ways you can access this:
是的 - Eloquent 有一个$table
变量。您可以通过两种方式访问它:
class yourModel extends Eloquent {
public static $table = "differentTable";
function someFunction()
{
return yourModel::$table;
}
}
or
或者
class yourModel extends Eloquent {
public function someFunction()
{
return $this->table();
}
}
then in your code
然后在你的代码中
Route::get('/', function () {
$model = new yourModel();
dd($model->someFunction());
});
回答by Flyn San
There is a public getTable() method defined in Eloquent\Modelso you should be able to use $model->getTable()
.
Eloquent\Model 中定义了一个公共 getTable() 方法,因此您应该能够使用$model->getTable()
.
回答by Lucky Soni
Taylor has an answerto your question:
泰勒回答你的问题:
Within the model class you can do something like this:
在模型类中,您可以执行以下操作:
return with(new static)->getTable();
If you want all your models to have the ability to return table name statically, then so something like this:
如果您希望所有模型都能够静态返回表名,那么如下所示:
class BaseModel extends Eloquent {
public static function getTableName()
{
return with(new static)->getTable();
}
}
class User extends BaseModel {
}
User::getTableName();
回答by sh6210
In my case, i'm using laravel 5.4
就我而言,我使用的是 laravel 5.4
return (new static)->getTable();
return (new static)->getTable();
回答by Lorenz Lo Sauer
Since table
is a protected property in the Model class (Laravel >= 5) you will need an instance of your Model.
由于table
是模型类中的受保护属性(Laravel >= 5),您将需要模型的实例。
Here is a case example:
下面是一个案例:
DB::table( (new YourModelClassname)->getTable() )
->update(['field' => false]);
回答by poring91
Based on Lucky Soni answer, there is another easy trick if you want to directly call it from Vontrolleror View.
根据Lucky Soni 的回答,如果您想直接从Vontroller或View调用它,还有另一个简单的技巧。
Tested in Laravel 6, and I keep using it, if you are "One Line Programmer"who hates extra line instance declaration. No need for extra lines in Model file too.
在 Laravel 6 中测试过,如果您是讨厌额外行实例声明的“单行程序员”,我会继续使用它。模型文件中也不需要额外的行。
$string_table_name = with(new \App\Model\TableModelName)->getTable();
or better you may also be able to just call this
或者更好的是,您也可以将其称为
$string_table_name = (new \App\Model\TableModelName)->getTable();
It will return plain string of the tabel name even if you rename $table
variable inside model class.
即使您$table
在模型类中重命名变量,它也会返回表格名称的纯字符串。
EDIT :
编辑 :
Minus Rep ?? Maybe you should try this first in your controllerinstead making new function in model class just to get table name and no need to declare the object when calling.
减去代表??也许您应该先在控制器中尝试这个,而不是在模型类中创建新函数来获取表名,而无需在调用时声明对象。
with()
itself is Laravel helper function that returns an object of the class. and inside class that extends Model, already has function getTable()
. So, you don't have to put another new redundant function inside model class.
It seems the latest version, you can just call (new Class)
without with()
function.
with()
本身是 Laravel 辅助函数,它返回类的对象。并且在扩展 Model 的内部类中,已经具有 function getTable()
。因此,您不必在模型类中放置另一个新的冗余函数。好像是最新版本,(new Class)
没有with()
功能就可以直接调用。
The difference between this answer and Lucky's answer, mine doesn't make any new function inside Model class to get the table name, even you can just call the function inside the Controller and View without declaring the object of model class. It's for beautify the code.
这个答案和 Lucky 的答案之间的区别,我的没有在 Model 类中创建任何新函数来获取表名,即使您可以在不声明模型类的对象的情况下调用 Controller 和 View 中的函数。这是为了美化代码。
While Lucky's answer create new function that inside Model class, and you need to call the function from the object.
虽然 Lucky 的答案在 Model 类中创建了新函数,但您需要从对象中调用该函数。
回答by Musa
I just wanted to add the following for people coming from search engines:
我只是想为来自搜索引擎的人添加以下内容:
In case you do not even want to instantiate the Model at all (faster?) :
如果您根本不想实例化模型(更快?):
$model = 'App\User';
$modelTable = str_replace('\', '', Str::snake(Str::plural(class_basename($model))));
dd($modelTable); // will return "users"
That might look ugly but that's exactly how the getTable() method resolves it under the hood, so...
这可能看起来很难看,但这正是 getTable() 方法在幕后解决它的方式,所以......
You will need to use Illuminate\Support\Str;
on top of your file.
您将需要use Illuminate\Support\Str;
在您的文件之上。
Addendum: implying you follow the framework's standards (i.e: Post
model has posts
table, User model has users
table, etc)
附录:暗示您遵循框架的标准(即:Post
模型有posts
表格,用户模型有users
表格等)
回答by Kirill Artemenko
In Laravel 4use staticmethod
在Laravel 4 中使用静态方法
$table_name = Model::getTable();
or "self" inside Eloquent Model
或Eloquent Model 中的“ self”
$table_name = self::getTable();