2

I'm newbie and I'm learning laravel 5.2, I have a blog that I'm making with laravel and I have a table in the database called "settings" and Here's the migration file of it:

public function up()
{
    Schema::create('settings', function (Blueprint $table) {
        $table->increments('id');
        $table->string('site_name');
        $table->text('site_description');
        $table->string('about_head');
        $table->text('about_description');
        $table->text('about_body');
        $table->timestamps();
    });
}

I entered data to this table, now I wanna pass these data to all controllers and all methods. I found that the best way to do that is to make a custom configuration file on App\Config folder. So I made a file called "custom" and I tried to access the database to get data from settings table and here's the custom config:

<?php
use Illuminate\Support\Facades\Config;
use App\Setting;

$setting = Setting::find(1);
return array(
  'site_name' => $setting->site_name,
  'site_description' => $setting->site_description,
  'about_head' => $setting->about_head,
  'about_description' => $setting->about_description,
  'about_body' => $setting->about_body,
);

But I got that error on my laravel root directory

Fatal error: Call to a member function connection() on null in C:\xampp\htdocs\myblog\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 3280

Have any idea how to fix it? or at least do you know another solution to pass my site settings data to all controllers, methods and views?

Thanks for your time.

Ahmed Essam
  • 1,000
  • 2
  • 8
  • 23

3 Answers3

0

Maybe adding the Connection Facade will resolve this issue -

<?php
use Illuminate\Database\Connection;
use Illuminate\Support\Facades\Config;
use App\Setting;

$setting = Setting::find(1);
return array(
  'site_name' => $setting->site_name,
  'site_description' => $setting->site_description,
  'about_head' => $setting->about_head,
  'about_description' => $setting->about_description,
  'about_body' => $setting->about_body,
);

It seems your model can't connect to the database, that's your main problem.

atefth
  • 1,624
  • 13
  • 26
0

I don't think query inside config file will work. What you can do is to create a service provider and use singleton. Create service provider:

php artisan make:provider MyConfigServiceProvider

Bind singleton:

$this->app->singleton('myconfig', function ($app) {
    return new MyConfig();
});

Use it:

$siteName = App::make('myconfig')->getConfig('site_name');
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Inside `MyConfig` I guess. ) – Alexey Mezenin Apr 29 '16 at 13:15
  • Sorry but I didn't see any good tutorial. People say there are some good related lessons at Laracasts, they're not free though. You could start from learning about service providers. https://laravel.com/docs/master/providers and MyConfig is just a custom classwith simple logic. – Alexey Mezenin Apr 29 '16 at 14:17
  • Thanks I found an answer on this link [link](http://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5) – Ahmed Essam Apr 29 '16 at 14:28
  • It uses also service provider I understood it thanks for your help bro – Ahmed Essam Apr 29 '16 at 14:28
0

I would suggest NOT to use database when you're using config. What I generally do is create a config file inside config folder (just like what you are doing) and hard code those values inside that config file.

P.S if your setting values are environment specific then put those values in env file and read those values in your config file

Abhishek
  • 3,900
  • 21
  • 42
  • This is a bad answer and here is why. There are many situations where you want to allow the end-user to change settings in the application via a UI. Hard-coding values into an environment file is not practical, maintainable, or convenient. – kjdion84 Aug 16 '17 at 00:05
  • 2
    "There are many situations where you want to allow the end-user to change settings in the application via a UI" - Well in that case don't use config. – Abhishek Aug 16 '17 at 07:30