gpt4 book ai didi

php - 使用 Illuminate Database 时在非对象上调用成员函数 connection() 时出错

转载 作者:行者123 更新时间:2023-11-29 04:21:41 26 4
gpt4 key购买 nike

我已经安装了带有 Composer 的 Sentry,因此它也安装了 Illuminate Database。安装成功,哨兵文档里的我都做了。我正在尝试使用简单的代码将用户添加到数据库中。但是它给了我这个错误信息:

Fatal error: Call to a member function connection() on a non-object in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\ilhan\vendor\illuminate\database\Illuminate\Database\Eloquent\Model.php on line 2472

我的代码如下:

<?php
include_once "vendor/autoload.php";


use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'ilhantestdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]);



use Cartalyst\Sentry\Sentry as Sentry;


try
{
$user = new Sentry;
// Create the user
$user->createUser(array(
'email' => 'john.doe@example.com',
'password' => 'test',
'activated' => true,
));

// Find the group using the group id
$adminGroup = Sentry::findGroupById(1);

// Assign the group to the user
$user->addGroup($adminGroup);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
echo 'User with this login already exists.';
}
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
{
echo 'Group was not found.';
}

连我都不知道怎么调试这个。此外,我认为由于 Illuminate 与 Sentry 一起提供,因此应该对 Sentry 进行编码以处理 Illuminate,因此我不需要太多配置。文档很差,我无法找到如何处理这个错误。

最佳答案

您还必须启动 ORM。文档对此并不清楚。

use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;

$capsule->addConnection([
...
]);

$capsule->bootEloquent();

关于php - 使用 Illuminate Database 时在非对象上调用成员函数 connection() 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21850710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com