19

I want to store user configuration data on database and I'm following this forum thread about it http://forumsarchive.laravel.io/viewtopic.php?id=10406 but when I implemented it laravel throws an error. I ran composer dump-autoload but nothing seems to be workign.What's the problem here?

// filename: app/config/settings.php

use \App\Models\Setting

$list = array();

$format = function(&$list, $keys, $val) use(&$format) {
    $keys ? $format($list[array_shift($keys)], $keys, $val) : $list = $val;
};

foreach(Setting::all() as $setting) {
    $format($list, explode('.', $setting->token), $setting->content);
}

return $list;

Usage:

echo Config::get('settings.token'); // returns value of 'content'

Full error

Fatal error: Call to a member function connection() on a non-object in C:\wamp\www\iapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 3137
Call Stack
#   Time    Memory  Function    Location
1   0.0017  247544  {main}( )   ..\index.php:0
2   0.0910  2848480 Illuminate\Foundation\Http\Kernel->handle( )    ..\index.php:58
3   0.0910  2848736 Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter( )  ..\Kernel.php:86
4   0.0917  2886304 Illuminate\Foundation\Http\Kernel->bootstrap( ) ..\Kernel.php:110
5   0.0917  2886472 Illuminate\Foundation\Application->bootstrapWith( ) ..\Kernel.php:215
6   0.0974  2994336 Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap( ) ..\Application.php:194
7   0.0986  3025160 Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles( )    ..\LoadConfiguration.php:38
8   0.1407  3814624 require( 'C:\wamp\www\iapp\config\settings.php' )   ..\LoadConfiguration.php:56
9   0.1407  3814696 ConfigSetting::getSettings( )   ..\settings.php:28
10  0.1494  4488840 Illuminate\Database\Eloquent\Model::all( )  ..\settings.php:16
11  0.1496  4494520 Illuminate\Database\Eloquent\Model->newQuery( ) ..\Model.php:646
12  0.1496  4494616 Illuminate\Database\Eloquent\Model->newQueryWithoutScopes( )    ..\Model.php:1769
13  0.1496  4494688 Illuminate\Database\Eloquent\Model->newBaseQueryBuilder( )  ..\Model.php:1795
14  0.1496  4494736 Illuminate\Database\Eloquent\Model->getConnection( )    ..\Model.php:1852
15  0.1496  4494784 Illuminate\Database\Eloquent\Model::resolveConnection( )

Edit :

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Setting extends Model {

}

3
  • What does the App\Models\Setting model look like? Can this model communicate with your database? Can you include the code in your question? Commented Jun 28, 2015 at 18:16
  • It looks like you have a database connection problem. Can this model communicate with your database? Commented Jun 28, 2015 at 18:31
  • It works on every other places like normal routes and controller but doesn't work here on config files.There is a question about it but it's only half answered. stackoverflow.com/questions/29203532/… Commented Jun 28, 2015 at 18:33

4 Answers 4

42

While running TestCase in Laravel 6.x and above, I often encounter this error. I realized that the unit test is extended from PHPUnit TestClass rather than Laravel TestCase. So change PHPUnit\Framework\TestCase; to Tests\TestCase.

<?php


use Tests\TestCase;

class ArtifactTest extends TestCase{

}

Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh! Thank you! I banged my head against the wall for over an hour trying to debug this error and it was because of that. You could probably move this to it's own Question and Answer but add some more key words to help people find it.
For those looking for the Pest equivalent: make sure to add uses(TestCase::class)->group('group name'); too
29

go to bootstrap/app.php

Just uncomment

$app->withEloquent();

4 Comments

thaks, i found it on lumen but not on laravel
I'm wondering why enabling eloquent is an option, but a major component of a framework (models) are totally dependant to Eloquent! They made this feature optional to beat other micro frameworks in benchmarks!
hi @yussan, please make sure that you use Laravel 5 instead of Laravel 4
of course @alexanderarda now i'm using Lumen with Laravel 5 base
9

Working with models in your settings files is not the best way to use them.

Your problem is, that your model query started BEFORE Laravel started it's services. That's why, when you try to make your query, model can't resolve it's connection, because DB service hasn't been initiated.

If you want do this stuff, create your own ServiceProvider and update your config there, or do it right in the boot method of your existing AppServiceProvider.

1 Comment

"Your problem is, that your model query started BEFORE Laravel started it's services. That's why, when you try to make your query, model can't resolve it's connection, because DB service hasn't been initiated." - That's the perfect answer! Just realized it.
0

I'm not entirely sure if this answer is in relation to the question, but I had the same error and fixed it with composer dump-autoload

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.