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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)