使用 Laravel 的 Eloquent ORM 在 Slim 中出现 null 错误时调用成员函数 connection()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38256812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:06:58  来源:igfitidea点击:

Call to a member function connection() on null error in Slim using Laravel's Eloquent ORM

laravelormeloquentslim

提问by Kennedy Osaze

I'm trying to make use of Laravel's Eloquent ORM on Slim microframework, but I've been seeing the error: Call to a member function connection() on null

我正在尝试在 Slim 微框架上使用 Laravel 的 Eloquent ORM,但我一直看到错误:Call to a member function connection() on null

Here is the code:

这是代码:

dependency.php

依赖.php

$container['db'] = function($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container->get('settings')['database']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

User.php (Model class)

User.php(模型类)

use Illuminate\Database\Eloquent\Model as Model;

class User extends Model {

    protected $table = "users";

    protected $fillable = ['name', 'email', 'password'];
}

HomeController.php (Controller class)

HomeController.php(控制器类)

class Home extends Controller {

public function index($request, $response, $args) {
        $user = User::find(1);
        var_dump($user);
        die();
        $title = "Slim Auth";
        $response = $this->view->render($response, 'home.php', ["title" => $title]);
        return $response;
    }
}

dependency.php was required into my bootstrap.php file where the Slim class was instantiated thus:

我的 bootstrap.php 文件中需要dependency.php,在该文件中实例化了 Slim 类:

$config = [
    'settings' => [
        'displayErrorDetails' => true,

        'view' => [
            'view_path' => APP_PATH . 'views/'
        ],

        'database' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'tutorial_slim_auth',
            'username'  => 'root',
            'password'  => 'passw0rd',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ],
    ]
];

$app = new Slim\App($config);

while HomeController.php and User.php were autoloaded via composer json file. After running the index.php (which also included my bootstrap.php) file containing:

而 HomeController.php 和 User.php 是通过 composer json 文件自动加载的。运行 index.php(也包括我的 bootstrap.php)文件后,包含:

$app->run();

this gave me a fatal error: Call to a member function connection() on null. But doing this:

这给了我一个致命错误:在 null 上调用成员函数 connection()。但是这样做:

echo '<pre>';
print_r($container['db']);
echo '</pre>';

in my bootstrap file produced the expected result along with the result of the var_dump function called in the HomeController.php file. What do I do? Or is there anything I'm not doing right?

在我的引导文件中产生了预期的结果以及在 HomeController.php 文件中调用的 var_dump 函数的结果。我该怎么办?或者有什么我做错的地方?

回答by Ildar Akhmetzyanov

Your global $capsule encapsulated here:

您的全局 $capsule 封装在这里:

$container['db'] = function($container) {
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($container->get('settings')['database']);

    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

Try this:

尝试这个:

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container['db'] = function ($container) use ($capsule) {
    return $capsule;
};

The reason the first version does not work is that the function is only called if you actually use the capsule, i.e., $this->db. No call is made, if you only use an Eloquent model class.

第一个版本不起作用的原因是该函数仅在您实际使用胶囊时才被调用,即$this->db. 如果您只使用 Eloquent 模型类,则不会进行调用。

回答by wowissu

On multiple connection , try this:

在多个连接上,试试这个:

$dbconfig = $ci->settings['db'];

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

foreach ($dbconfig['connections'] as $cname => $connection)
{
    $cname = $cname == $dbconfig['default'] ? 'default' : $cname;

    $capsule->addConnection($connection, $cname);
}

$capsule->setFetchMode($dbconfig['fetch']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$ci['db'] = function ($ci) use($capsule) {
    return $capsule;
};

then

然后

DB::connection('other_connection')->table('...')->get();