mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Lumen: BindingResolutionException #148

Closed JacerOmri closed 6 years ago

JacerOmri commented 6 years ago

Describe the bug After a regular install of the package on a Lumen project, and added the caching trait to models, some routes throw an exception

Target [Illuminate\Database\ConnectionInterface] is not instantiable while building [Illuminate\Database\Query\Builder].

Eloquent Query

$model->all();

Stack Trace

[2018-07-17 19:12:07] lumen.ERROR: Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Database\ConnectionInterface] is not instantiable while building [Illuminate\Database\Query\Builder]. in /path/api/vendor/illuminate/container/Container.php:918
Stack trace:
#0 /path/api/vendor/illuminate/container/Container.php(758): Illuminate\Container\Container->notInstantiable('Illuminate\\Data...')
#1 /path/api/vendor/illuminate/container/Container.php(631): Illuminate\Container\Container->build('Illuminate\\Data...')
#2 /path/api/vendor/illuminate/container/Container.php(586): Illuminate\Container\Container->resolve('Illuminate\\Data...', Array)
#3 /path/api/vendor/laravel/lumen-framework/src/Application.php(230): Illuminate\Container\Container->make('Illuminate\\Data...', Array)
#4 /path/api/vendor/illuminate/container/Container.php(885): Laravel\Lumen\Application->make('Illuminate\\Data...')
#5 /path/api/vendor/illuminate/container/Container.php(813): Illuminate\Container\Container->resolveClass(Object(ReflectionParameter))
#6 /path/api/vendor/illuminate/container/Container.php(780): Illuminate\Container\Container->resolveDependencies(Array)
#7 /path/api/vendor/illuminate/container/Container.php(631): Illuminate\Container\Container->build('Illuminate\\Data...')
#8 /path/api/vendor/illuminate/container/Container.php(586): Illuminate\Container\Container->resolve('Illuminate\\Data...', Array)
#9 /path/api/vendor/laravel/lumen-framework/src/Application.php(230): Illuminate\Container\Container->make('Illuminate\\Data...', Array)
#10 /path/api/vendor/laravel/lumen-framework/src/helpers.php(38): Laravel\Lumen\Application->make('Illuminate\\Data...')
#11 /path/api/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(83): app('Illuminate\\Data...')
#12 /path/api/vendor/genealabs/laravel-model-caching/src/Traits/ModelCaching.php(17): App\BaseModel->makeCacheTags()
#13 /path/api/app/Services/WordReferenceService.php(24): App\BaseModel::all()
#14 /path/api/app/Http/Controllers/languageWord_CRUD_Controller.php(132): App\Services\WordReferenceService->getAllReferences()
#15 [internal function]: App\Http\Controllers\languageWord_CRUD_Controller->getWordFiltredForTraductionFrontOffice(Object(Illuminate\Http\Request), '80')
#16 /path/api/vendor/illuminate/container/BoundMethod.php(29): call_user_func_array(Array, Array)
#17 /path/api/vendor/illuminate/container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#18 /path/api/vendor/illuminate/container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Laravel\Lumen\Application), Array, Object(Closure))
#19 /path/api/vendor/illuminate/container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Laravel\Lumen\Application), Array, Array, NULL)
#20 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(373): Illuminate\Container\Container->call(Array, Array)
#21 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(339): Laravel\Lumen\Application->callControllerCallable(Array, Array)
#22 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(313): Laravel\Lumen\Application->callLumenController(Object(App\Http\Controllers\languageWord_CRUD_Controller), 'getWordFiltredF...', Array)
#23 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(275): Laravel\Lumen\Application->callControllerAction(Array)
#24 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(260): Laravel\Lumen\Application->callActionOnArrayBasedRoute(Array)
#25 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(230): Laravel\Lumen\Application->handleFoundRoute(Array)
#26 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(164): Laravel\Lumen\Application->handleDispatcherResponse(Array)
#27 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Illuminate\Http\Request))
#28 /path/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#29 /path/api/app/Http/Middleware/CorsMiddleware.php(30): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Illuminate\Http\Request))
#30 /path/api/vendor/illuminate/pipeline/Pipeline.php(149): App\Http\Middleware\CorsMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 /path/api/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#33 /path/api/vendor/illuminate/pipeline/Pipeline.php(102): Laravel\Lumen\Routing\Pipeline->Laravel\Lumen\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(410): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#35 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(166): Laravel\Lumen\Application->sendThroughPipeline(Array, Object(Closure))
#36 /path/api/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(107): Laravel\Lumen\Application->dispatch(NULL)
#37 /path/api/public/index.php(31): Laravel\Lumen\Application->run()
#38 {main}

Environment

Additional context This occurs on controllers with few operations (no to few middlewares). I think there is some binding that is assumed to be happening, while it is not. I use redis. I also use Eloquent, Facades and added cosmicvelocity/lumen-helpers to define cache() helper.

mikebronner commented 6 years ago

@JacerOmri Thanks for reporting this. I will try to take a look at this, but currently don't have a lumen project to test this in. Could you post a repo that has the problem, so I can clone and replicate the issue, and look for a solution? Thanks! :)

mikebronner commented 6 years ago

@JacerOmri just checking in, would you be able to link to a repository I can download and test this with, where this problem occurs? Thanks!

JacerOmri commented 6 years ago

I will add a repo showing this error soon. Thank you

getriebesand commented 6 years ago

In Lumen there is a missing alias db.connection

you can fix that by adding this to your register method in your AppServiceProvider:

$this->app->bind('db.connection', function ($app) {
    return $app['db']->connection();
});

$this->app->alias('db.connection', \Illuminate\Database\ConnectionInterface::class);
mikebronner commented 6 years ago

I believe this will be fixed in the next release: #156

mikebronner commented 6 years ago

Closing this for now due to inactivity. Please re-open with additional information if you are still experiencing this problem.