有没有一种方法可以让我所在的模型正在使用当前数据库表?我看到 Laravel/Database/Eloquent/model.php 中有一个 table() 函数,但我一直没有成功调用它从我所在的模型中调用它。
15 回答
Eloquent\Model中定义了一个公共 getTable() 方法,因此您应该可以使用$model->getTable()
.
泰勒回答了你的问题:
在模型类中,您可以执行以下操作:
return with(new static)->getTable();
如果您希望所有模型都能够静态返回表名,那么就像这样:
class BaseModel extends Eloquent {
public static function getTableName()
{
return with(new static)->getTable();
}
}
class User extends BaseModel {
}
User::getTableName();
2019 年 4 月编辑:此答案现已过时。查看 Flyn San 的新正确答案
是的 - Eloquent 有一个$table
变量。您可以通过两种方式访问它:
class yourModel extends Eloquent {
public static $table = "differentTable";
function someFunction()
{
return yourModel::$table;
}
}
或者
class yourModel extends Eloquent {
public function someFunction()
{
return $this->table();
}
}
然后在你的代码中
Route::get('/', function () {
$model = new yourModel();
dd($model->someFunction());
});
就我而言,我使用的是 laravel 5.4
return (new static)->getTable();
由于table
是 Model 类(Laravel >= 5)中的受保护属性,因此您将需要 Model 的实例。
这是一个案例示例:
DB::table( (new YourModelClassname)->getTable() )
->update(['field' => false]);
您可以通过以下代码获取模型表的名称:
如果我们有一个 Model 作为 ModelName:
ModelName::query()->getQuery()->from
在模型中定义的自定义表名的情况下,此方法也可以正常工作protected $table = 'custom_table_name'
。
根据Lucky Soni 的回答,如果您想直接从Vontroller或View调用它,还有另一个简单的技巧。
在 Laravel 6 中测试,如果您是讨厌额外行实例声明的“单行程序员” ,我会继续使用它。模型文件中也不需要额外的行。
$string_table_name = with(new \App\Model\TableModelName)->getTable();
或者更好的是,您也可以只调用它
$string_table_name = (new \App\Model\TableModelName)->getTable();
即使您$table
在模型类中重命名变量,它也会返回表格名称的纯字符串。
编辑 :
减去代表??也许你应该首先在你的控制器中尝试这个,而不是在模型类中创建新函数只是为了获取表名并且在调用时不需要声明对象。
with()
本身是 Laravel 辅助函数,它返回类的对象。并且扩展模型的内部类已经具有功能getTable()
。因此,您不必在模型类中添加另一个新的冗余函数。好像是最新版本,直接调用(new Class)
就可以了with()
。
这个答案和 Lucky 的答案之间的区别,我的没有在 Model 类中创建任何新函数来获取表名,即使你可以只调用 Controller 和 View 中的函数而不声明模型类的对象。这是为了美化代码。
虽然 Lucky 的答案在 Model 类中创建了新函数,但您需要从对象中调用该函数。
通过以下方式从 Laravel 模型中获取表名的简单方法:
$tableName = app(\App\User::class)->getTable();
不要忘记更换:
\应用\用户
带模型路径。
它将从模型中返回表名。在 laravel 8 上完美运行
app(Modelname::class)->getTable()
你必须用你的模型类替换 Modelname
这是另一种方法,您可以静态获取模型的表名。
- 定义一个特征:
app/Traits/CanGetTableNameStatically.php
<?php namespace App\Traits;
trait CanGetTableNameStatically
{
public static function tableName()
{
return (new static)->getTable();
}
}
- 扩展您的要求
Model
或BaseModel
与use
声明。
app/Models/BaseModel.php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\CanGetTableNameStatically;
class BaseModel extends Model
{
use CanGetTableNameStatically;
// ...
}
在您的模型上,如果您在 Laravel 的保留属性上设置自定义表名:protected $table
那么它仍然可以工作并返回正确的表名。
app/Models/Customer.php
<?php namespace App\Models\Master;
use App\Models\BaseModel;
class Customer extends BaseModel
{
protected $table = 'my_customers';
// ...
}
用法:随便打电话YourModel::tableName()
。
在视图中:
{{ \App\Models\Customer::tableName() }}
进行联接时:
DB::table( Product::tableName() . ' AS p' )
->leftJoin( ProductCategory::tableName() . ' AS pc', 'pc.id', '=', 'p.category_id')
// ... etc
注意:我在需要但完全公开的地方使用了这种方法,我在这里找到了另一个具有完全相同方法的答案,所以我复制粘贴在这里以供参考,当然要感谢@topher
另一种解决方案是resolve
像这样使用助手:
resolve('\\App\\Models\\User')->getTable()
如果您使用的是表名前缀,那么到目前为止,所有答案都不会为您提供带有前缀的表名。这时候,如果我们想要数据库表的真实名称,似乎需要我们自己将前缀与表名连接起来。
以下是获取包含表前缀的表名的方法:
echo \App\MyModel::query()->getQuery()->getGrammar()->getTablePrefix() . app(\App\MyModel::class)->getTable();
我只想为来自搜索引擎的人添加以下内容:
如果您根本不想实例化模型(更快?):
$model = 'App\User';
$modelTable = str_replace('\\', '', Str::snake(Str::plural(class_basename($model))));
dd($modelTable); // will return "users"
这可能看起来很难看,但这正是 getTable() 方法在后台解决它的方式,所以......
您将需要use Illuminate\Support\Str;
在您的文件之上。
附录:暗示您遵循框架的标准(即:Post
模型有posts
表,用户模型有users
表等)
在Laravel 4 中使用静态方法
$table_name = Model::getTable();
或Eloquent 模型中的“ self ”
$table_name = self::getTable();