我不确定我需要做什么才能使动态表名正常工作。
考虑以下模型(表“测试”不存在):
<?php
// app/models/Test.php
class Test extends Eloquent {
}
然后如果我这样做(“字段”表确实存在):
<?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 构造函数中,尽管我无法让它工作按照那个例子。
感谢帮助。