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

后端 未结 5 1104
长发绾君心
长发绾君心 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:22

    In case you're working with Lumen, you may occur identical problem. In this case just uncomment:

    // $app->withFacades();
    
    // $app->withEloquent();
    

    in bootstrap\app.php

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2021-02-13 12:26

    Answer:

    Bootstrap your package in your service provider's boot method.


    Explanation:

    Since you're developing a package to be used with Laravel, there's no point in making your own Capsule instance. You can just use Eloquent directly.

    Your problem seems to stem from DB/Eloquent not being set up yet by the time your code hits it.

    You have not shown us your service provider, but I'm guessing you're using one and doing it all in the register method.

    Since your package depends on a different service provider (DatabaseServiceProvider) to be wired up prior to its own execution, the correct place to bootstrap your package is in your service provider's boot method.

    Here's a quote from the docs:

    The register method is called immediately when the service provider is registered, while the boot command is only called right before a request is routed.

    So, if actions in your service provider rely on another service provider already being registered [...] you should use the boot method.

    0 讨论(0)
  • 2021-02-13 12:27

    What i did was simple, i just forgot to uncomment $app->withFacades(); $app->withEloquent(); in my bootstrap/app.php.

    Now works fine

    0 讨论(0)
  • 2021-02-13 12:46

    Try including the DB facade as well as Eloquent...

    use Illuminate\Support\Facades\DB;
    use Illuminate\Database\Eloquent\Model as Eloquent;
    

    ...and then see if you have access to DB::table('chat_history').

    (Also note that in your class, your call to use Illuminate\Database\Model; should be Illuminate\Database\Eloquent\Model; )

    0 讨论(0)
提交回复
热议问题