我在Wordpress中安装了Eloquent以进行数据库操作。我的composer.json文件如下所示:
{
"require": {
"illuminate/database": "~5.0.0",
"illuminate/events": "~5.0.0",
},
"autoload": {
"psr-4": {
"App\\": "wp-content/themes/tkh/src/"
}
}
}
我在eloquent.php中创建了数据库连接:
<?php
require __DIR__.'/../vendor/autoload.php';
/*
* Configure Eloquent (called Capsule when used alone)
*/
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'tkh',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher(new \Illuminate\Container\Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
现在大多数事情(保存,删除,hasMany等方法)似乎工作正常。但是在DB外观上调用静态方法不起作用。
在数据库外观上调用select方法的简化示例:
<?php
use Illuminate\Support\Facades\DB as DB;
if(isset($_POST['select']))
{
$schedule = DB::select('select 567567 from schedule where id = 77');
}
?>
<form action="" method="post">
<input type="submit" name="select" value="select"/>
</form>
当我点击选择时,我得到了这个致命的错误:
[28-Sep-2017 11:04:03 UTC] PHP Fatal error: Call to a member function select() on null in C:\xampp\htdocs\tkh\wp-content\themes\tkh\vendor\illuminate\support\Facades\Facade.php on line 210
[28-Sep-2017 11:04:03 UTC] PHP Stack trace:
[28-Sep-2017 11:04:03 UTC] PHP 1. {main}() C:\xampp\htdocs\tkh\index.php:0
[28-Sep-2017 11:04:03 UTC] PHP 2. require() C:\xampp\htdocs\tkh\index.php:17
[28-Sep-2017 11:04:03 UTC] PHP 3. require_once() C:\xampp\htdocs\tkh\wp-blog-header.php:19
[28-Sep-2017 11:04:03 UTC] PHP 4. include() C:\xampp\htdocs\tkh\wp-includes\template-loader.php:74
[28-Sep-2017 11:04:03 UTC] PHP 5. Illuminate\Support\Facades\DB::select() C:\xampp\htdocs\tkh\wp-content\themes\tkh\page-schedules.php:6
[28-Sep-2017 11:04:03 UTC] PHP 6. Illuminate\Support\Facades\Facade::__callStatic() C:\xampp\htdocs\tkh\wp-content\themes\tkh\page-schedules.php:6
在以下函数中,Illuminate \ Support \ Facades \ Facade中的案例1行失败:
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
switch (count($args))
{
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
当我调试那段代码时,我得到了这些本地变量:
Locals
$args array[1]
$args[0] "select 567567 from schedule where id = 77"
$instance null
$method "select"
::object [Illuminate\Support\Facades\Facade]
::app null
::resolvedInstancearray [1]
::resolvedInstance['db'] null
因此$ instance var为null并导致异常。我猜这是方法需要执行的db实例,我需要在我的eloquent.php文件中创建额外的东西,但是搜索和研究的时间没有发现任何东西,所以我想知道是否有人可以放弃一些点亮这个$ instance变量是什么以及如何填充它?
非常感谢!
答案 0 :(得分:0)
好的,在这里找到答案:https://github.com/illuminate/database
如果你想在没有Laravel的情况下运行Eloquent,请使用Capsule而不是DB facade,如下所示:
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$results = Capsule::select('select * from users where id = ?', array(1));