Laravel/Eloquent: Fatal error: Call to a member function connection() on a non-object

后端 未结 5 1105
长发绾君心
长发绾君心 2021-02-13 12:06

I\'m building a package in Laravel 4 but am getting a non-object error when attempting to access the db from which seems to be a properly instantiated object. Here\'s the setup:

5条回答
  •  别那么骄傲
    2021-02-13 12:24

    @matpop and @TonyStark were on the right track: Capsule\Manager wasn't being booted.

    use Illuminate\Database\Capsule\Manager as Capsule;
    
    $capsule = new Capsule;
    $capsule->addConnection([
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'project',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ]);
    
    // Set the event dispatcher used by Eloquent models... (optional)
    use Illuminate\Events\Dispatcher;
    use Illuminate\Container\Container;
    
    $capsule->setEventDispatcher(new Dispatcher(new Container));
    
    // Make this Capsule instance available globally via static methods... (optional)
    $capsule->setAsGlobal();
    
    // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
    $capsule->bootEloquent();
    

    I am able to extend Eloquent after booting. I think another solution might be along the lines of (but not tested):

    include __DIR__ . '/../../vendor/autoload.php';
    $app = require_once __DIR__ . '/../../bootstrap/start.php';
    $app->boot();
    

提交回复
热议问题