Luciano Fantuzzi Luciano Fantuzzi - 1 month ago 5x
PHP Question

What are the best practices for creatiang a "settings" model in Laravel 5?

This is my first project using Laravel (I'm starting with 5.2). I'm building a self managed website. There's an admin that should save site settings for using later in the website. Eg: Background color, social networks, carousel with pictures, products, etc.

My first try was creating a model "Setting" with: id | key (unique) | value. When I tried to save social networks (to display in the site header), I realized I'd have to save in "value" a json with URL, title, etc... Ugly implementation for a relational model, so I came to the conclussion it was bad. I created a sepparated model SocialNetwork with: id | URL | image | name.

Now I'm thinking in loading all these models at once in my BaseController so I can have it through all controllers, but I'm not sure it's correct. I mean, an array $settings with all models as part of it:

abstract class BaseController extends Controller
protected $settings;

public function __construct(...)
$this->settings = [
'backgroundColor' => 'red',
'socialNetworks' => SocialNetwork::all(),
'other' => Other::all(),

It looks horripilant to me, but I'm lost with this framework and I don't know what's the best I could do to deal with this.
Someone told me about service providers, but I'm trying to figure out the advantages of it and the best approach to be able to also made use of these models (to save data) and I don't get it.

I'm not asking for explicit code (it would be nice however), I'll investigate it after understanding the approach, but some tips or conceptual help would be really appreciated.

Thank you!


This is a great question. Recently i did for one of my Code Canyon product (Krisma CMS Script) and my approach was as below.

1) Created a Service Provider

public function boot()

Each of this function which is called in the boot is declared in the Provider Class below boot function. e.g for Header Section i fetched data from DB via Header Model and shared it globally which can be accessed any where in your app.

$section = Section ::find(1);
view()->share('section ', $section);

2) Created a table for each section i.e about, intro etc and inserted the values which i want to modify.

3) Now in the admin panel create a form for changes/modifications and update the desired section properties.

4) Fetch the data in your view. Suppose if the user has changed the background color from admin panel to RED, so print out that variable right in your view style : {{ $sectionname->color }}

5) That's it i hope it gives you a better idea of how settings can be implemented and i did this for my Product, may be there is a better way but up to now this is what i found in my research and works perfect for me.