我不确定我需要做什么才能使动态表名起作用.
考虑以下模型(表'test'不存在):
<?php
// app/models/Test.php
class Test extends Eloquent {
}
如果我这样做('fields'表确实存在):
<?php
// app/routes.php
$test = new \Test;
$test->setTable('fields');
$data = $test->find(1);
dd($data);
我收到一个错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.tests' doesn't exist (SQL: select * from `tests` where `id` = ? limit 1) (Bindings: array ( 0 => 1, ))"
请注意,从Test模型设置表名称可以正常工作.
L4实际上使用了setTable()方法,非常类似于我想要的方式,在vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations.Pivot.php构造函数中,虽然我无法让它工作遵循那个例子.
感谢帮助.