[SOLVED] Use Slim with Eloquent: Call to a member function connection() on null

Hi, i want to use eloquent with slim but it does not work…

My Config:
[
‘settings’ => [
‘determineRouteBeforeAppMiddleware’ => false,
‘displayErrorDetails’ => true,
‘db’ => [
‘driver’ => ‘mysql’,
‘host’ => ‘localhost’,
‘database’ => ‘…’,
‘username’ => ‘…’,
‘password’ => ‘…’,
‘charset’ => ‘utf8’,
‘collation’ => ‘utf8_unicode_ci’,
‘prefix’ => ‘’,
]
],
]

Dependencies:
$container[‘db’] = function ($container) {
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container[‘settings’][‘db’]);

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

    return $capsule;
};

My index file:
require ‘vendor/autoload.php’;

// Config
$settings = require 'config/settings.php';

// Models
include "models/news.php";

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

// Dependencies
require 'config/dependencies.php';

// Routing
require 'api/newsController.php';

$app->run();

My routing function:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \App\News;

$app->get('/api/v1/news', function (Request $request, Response $response) {
    $news = News::all();
    return $response;
});

And at least my model:
namespace App;

use Illuminate\Database\Eloquent\Model as Model;

class News extends Model {

    protected $table = "news";

}

I got the following message:
Type: Error
Message: Call to a member function connection() on null
File: /www/htdocs/w0123a251/sm2/vendor/illuminate/database/Eloquent/Model.php
Line: 3303
Trace

#0 /www/htdocs/w006a525/sm2/vendor/illuminate/database/Eloquent/Model.php(3269): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 /www/htdocs/w006a525/sm2/vendor/illuminate/database/Eloquent/Model.php(1858): Illuminate\Database\Eloquent\Model->getConnection()
#2 /www/htdocs/w006a525/sm2/vendor/illuminate/database/Eloquent/Model.php(1831): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 /www/htdocs/w006a525/sm2/vendor/illuminate/database/Eloquent/Model.php(1801): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#4 /www/htdocs/w006a525/sm2/vendor/illuminate/database/Eloquent/Model.php(632): Illuminate\Database\Eloquent\Model->newQuery()
#5 /www/htdocs/w006a525/sm2/api/newsController.php(11): Illuminate\Database\Eloquent\Model::all()
#6 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#7 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#8 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/Route.php(325): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#9 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(116): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/Route.php(297): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/App.php(443): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(116): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/App.php(337): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#14 /www/htdocs/w006a525/sm2/vendor/slim/slim/Slim/App.php(298): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#15 /www/htdocs/w006a525/sm2/index.php(19): Slim\App->run()
#16 {main}

I dont know how to fix this :frowning:

Found the solution here: Eloquent initialization error

:smile:

Thanks a Lot :smile:
this works for me