[英]Capsule manager table prefixes not being used
It's a Laravel website, just having a problem with table prefixes. 这是一个Laravel网站,只是表前缀有问题。
It has a MySQL table of static pages called cleaning_pages
and in public/index.php
I've put the following just before the Kernel
's instantiation: 它有一个静态表的MySQL表,称为cleaning_pages
,在public/index.php
我在Kernel
实例化之前放置了以下内容:
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'somehost.com',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'port' => 3306,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'cleaning_',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
I've given it the prefix cleaning_
to save on having ugly class names. 我给它加上了前缀cleaning_
以节省具有丑陋的类名。 I then made a new Eloquent model class called Page
, but then if I call it statically elsewhere like so: 然后,我创建了一个新的Eloquent模型类,称为Page
,但是如果我在其他地方静态调用它,如下所示:
public function fetchPage($slug)
{
return \App\Models\Page::where('page_slug', '=', $slug)->get();
}
It throws a QueryException
: 它抛出QueryException
:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.pages' doesn't exist
But if, just purely for the sake of testing this, I put the following right in the index.php
file right after the $capsule
stuff from above: 但是,如果仅出于测试目的,我将以下内容放在上面$capsule
之后的index.php
文件中:
print_r(\App\Models\Page::where('page_slug', '=', $slug)->get());
It queries the right table fine. 它查询正确的表。 I thought that the Manager::setAsGlobal()
method was supposed to allow global static access throughout? 我以为Manager::setAsGlobal()
方法应该允许全局静态访问?
I'm a dumbass. 我是笨蛋 I needed to set the 'prefix' in config/database.php
, not just in the Manager
instance. 我需要在config/database.php
设置“前缀”,而不仅仅是在Manager
实例中设置。 But this raises a further question - does the database config always override any Manager
instance you make, even if you set it as global? 但这又引发了一个问题-数据库配置是否总是覆盖您创建的任何Manager
实例,即使您将其设置为全局实例也是如此? Or am I not understanding this right? 还是我不明白这个权利?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.