更新表名在运行时不工作 - laravel雄辩ORM [英] Update the table name at runtime not working - laravel Eloquent ORM
本文介绍了更新表名在运行时不工作 - laravel雄辩ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的模型:
class Product extends \GlobalModel {
protected $table = 'product';
}
我想更新表名称 oops_product
我发现 getTable(); 从模型中获取表名,其工作正常:
I found getTable(); to get the table name from model and its working fine:
$tableName = with(new Product)->getTable();
但是当我使用 setTable(); 设置表名时 GitHub 解决方案,它不更新表名。
But when i set the table name using setTable(); as per GitHub solution, its not updating the table name.
with(new Product)->setTable("oops_produdct");
有什么问题吗?
帮助将不胜感激。
编辑:
$product = new Product();
$product->getTable(); // output: product
$product->setTable("oops_product");
$product->getTable(); // output: oops_product
现在当我运行这个
$product->all();
它执行
"select * from product"
而不是
"select * from oops_product"
推荐答案
all()
是一种静态方法,使用全新的实例,并调用 get()
就可以了。
all()
is a static method that uses brand new instance and calls get()
on it.
所以你只需要使用正确的方法:
So all you need is using proper method:
$product = new Product;
$product->getTable(); // products
$product->setTable('oooops');
$product->get(); // select * from oooops
$product->first(); // select * from oooops limit 1
etc...
只是避免使用静态的口才方法,因为它们显然创建了新的实例,它将具有默认的表
属性。
Just avoid using static Eloquent methods, since they obviously create new instance, that will have default table
property.
这篇关于更新表名在运行时不工作 - laravel雄辩ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文