dongmu1996 2016-05-26 06:48
浏览 51
已采纳

Slim 3 Eloquent初始化错误

Disclosure: This is my first Slim app

I've tried to use Eloquent with Slim 3, but I cant seem to get it work. I've installed everything with composer, followed the install guide on the site.

When I try to use my User model (wich extends the Eloquent model) i get a fatal error: Fatal error: Call to a member function connection() on null in ****\vendor\illuminate\database\Eloquent\Model.php on line 3280

My appConfig looks like this:

$config['db'] = [
    'driver'    => 'mysql',
    'host'      => '****',
    'database'  => '****',
    'username'  => '****',
    'password'  => '****',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
];

which i pass to the app according to the documentation:

$app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();
$container['db'] = function (ContainerInterface $container) {
    $settings = $container->get('database');
    $capsule = new \Illuminate\Database\Capsule\Manager;
    $capsule->addConnection($settings);
    $capsule->setAsGlobal();
    $capsule->bootEloquent();

    return $capsule;
};

My User class looks like this:

class User extends Illuminate\Database\Eloquent\Model {}

I've found a workaround on Stackoverflow like so:

use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Capsule\Manager;

class User extends Eloquent {
        public function __construct(Manager $capsule, array $attributes = [])
    {
        parent::__construct($attributes);
    }
}

But if i override the constructor according to the port i get a different error:

_Catchable fatal error: Argument 1 passed to Up\models\User::__construct() must be an instance of Illuminate\Database\Capsule\Manager, none given, called in ****\vendor\illuminate\database\Eloquent\Model.php on line 644 and defined in ****\models\User.php on line 14_

I'm stuck now. Please help

  • 写回答

1条回答 默认 最新

  • douna1895 2016-05-26 07:38
    关注

    I've found a solution here, but it seems a little out of place. I just had to init Eloquent separetly like so:

    $capsule = new Capsule;
    $capsule->addConnection($config['db']);
    $capsule->setEventDispatcher(new Dispatcher(new Container));
    $capsule->bootEloquent();
    

    this way the $container['db'] = function () {} part is excluded, but now everything works

    EDIT (for comments): the whole part now looks like this:

    $app = new \Slim\App(["settings" => $config]);
    $container = $app->getContainer();
    /*$container['db'] = function (ContainerInterface $container) {
        $settings = $container->get('database');
        $capsule = new \Illuminate\Database\Capsule\Manager;
        $capsule->addConnection($settings);
        $capsule->setAsGlobal();
        $capsule->bootEloquent();
    
        return $capsule;
    };*/
    
    $capsule = new Capsule;
    $capsule->addConnection($config['db']);
    $capsule->setEventDispatcher(new Dispatcher(new Container));
    $capsule->bootEloquent();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题