Laravel - Set global variable from settings table

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel: Storing Global Settings from the Database – A Complete Guide

As a senior developer working with Laravel, managing application settings is a common requirement. You want your configuration—things like site titles, social media handles, or feature flags—to be persistent and easily accessible across your application, whether in Blade views, controllers, or services. The challenge you are facing involves bridging the gap between your database (Eloquent models) and global application state.

This post will walk you through the correct, robust way to store settings from your settings table and make them fully accessible throughout your Laravel application, addressing the issues found in setting up your update mechanism. We will focus on proper Eloquent usage and service layering.

The Foundation: Retrieving Settings with Eloquent

The first step is always ensuring you can reliably fetch the data you need. Since you have correctly defined your Setting model, accessing this data becomes straightforward using Eloquent. When dealing with relational data in Laravel, understanding how to query and manipulate these relationships is key. For more advanced knowledge on database interactions and Eloquent features, check out the official documentation at https://laravelcompany.com.

To make settings globally accessible, you typically implement a dedicated service class or repository pattern rather than trying to dump everything directly into static variables. This keeps your application decoupled and testable.

Implementing a Setting Repository

Your attempt to use a repository is the right architectural choice. Let’s refine how the repository interacts with the model to ensure data persistence during updates.

// app/Repositories/SettingRepository.php

use App\Models\Setting;

class SettingRepository
{
    protected $model;

    public function __construct(Setting $model)
    {
        $this->model = $model;
    }

    /**
     * Finds a setting by its name.
     */
    public function findByName(string $name): ?Setting
    {
        // Use find() instead of update() if you need the model instance back, 
        // or use update() for mass updates (see below).
        return $this->model->where('name', $name)->first();
    }

    /**
     * Updates a setting record.
     */
    public function update(string $name, array $data): bool
    {
        $setting = $this->model->where('name', $name)->first();

        if ($setting) {
            $setting->fill($data);
            $setting->save();
            return true;
        }

        return false;
    }
}

Notice how the update method now explicitly finds the record, fills the attributes, and saves it. This is much safer than relying solely on raw query methods when dealing with model persistence.

Fixing the Update Flow in the Controller

The failure point often lies in how data flows between the controller, repository, and view. In your controller, you should inject the repository and handle the request data explicitly. We will use Laravel's built-in validation and mass assignment features for a cleaner update process.

// app/Http/Controllers/Admin/AdminConfiguracoesController.php

use App\Repositories\SettingRepository;
use Illuminate\Http\Request;

class AdminConfiguracoesController extends AdminBaseController
{
    private $repository;

    public function __construct(SettingRepository $repository)
    {
        $this->repository = $repository;
    }

    public function update(Request $request, string $settingName) // Pass the setting name from the route
    {
        // 1. Validate incoming data
        $validatedData = $request->validate([
            'title' => 'required|string|max:255',
            'facebook' => 'nullable|string',
            'twitter' => 'nullable|string',
            'instagram' => 'nullable|string',
        ]);

        // 2. Use the repository to update the record
        $success = $this->repository->update($settingName, $validatedData);

        if ($success) {
            return redirect('admin')->with('success', "Setting updated successfully!");
        } else {
            return back()->withErrors(['general' => 'Could not find the setting to update.']);
        }
    }
}

By incorporating validation and ensuring the repository performs a proper save() operation, you guarantee that data is correctly written to your database, which is the most reliable way to manage application state in a Laravel environment.

Conclusion

Storing global settings from a database table is an excellent practice for dynamic applications. The key takeaway is to avoid treating Eloquent models as simple data containers and instead leverage the power of service layers (like your SettingRepository) to encapsulate complex business logic, such as finding, updating, and saving records. By strictly following patterns like those promoted by Laravel, you ensure that your application remains clean, maintainable, and highly scalable. For deeper explorations into architectural patterns within Laravel, always refer back to the comprehensive resources available at https://laravelcompany.com.