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:
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 theboot
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.