Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

madsem's avatar

Best place to write Session data to remember UI settings (html select fields etc)

So I'm new to laravel still and writing an admin backend, and right now I want that certain settings are remembered (for example options selected in a html select field) when I browse to another page.

One example would be "results per page", which is a partial blade view:

<form method="post" action="">
    <div class="input-field col s12">
        <select name="results_per_page">
            <option value="15">15</option>
            <option value="50">50</option>
            <option value="100">100</option>
            <option value="250">250</option>
            <option value="500">500</option>
            <option value="1000">1000</option>
        </select>
        <label>Results Per Page</label>
    </div>
</form>

What would be the best place in laravel to write the code that adds vars to session, specifically for this purpose?

I am trying to figure out the best and most logical way so that results per page, or also other settings are remembered for the duration of my session, and are available everywhere.

Without having to duplicate the code in every controller.

Any ideas?

0 likes
6 replies
martinbean's avatar

@madsem I would be tempted to serialise settings and then store them against the user in the database. These settings will then be available when authenticated.

You can add methods to your User model to read the values of specific settings:

class User extends Authenticatable
{
    public function setting($key, $default = null)
    {
        return data_get($this->settings, $key, $default);
    }
}

Assuming your settings column is set to be array or json so it’s automatically serialised and deserialised.

1 like
Thyrosis's avatar

I agree with Martin's comment. Store it in a database. Nothing as annoying to have to select 'show this many per page' every time I sign in on a forum I'm on.

Side note on the storing as json: You'll need mysql 5.7 for that, which is not widely supported yet I don't think. MariaDB 10.1 should support it too, but I haven't been able to get it to work on there either. I just store the json encoded options as text and decode them whenever needed.

1 like
madsem's avatar

Thanks guys, this is what I will do. Makes a lot of sense to put this in the User class.

madsem's avatar

@martinbean one more question actually :) Hope you don't mind.

So, I've added this to User:

/**
     * get UI setting for user
     * @param $key
     * @param null $default
     * @return mixed
     */
    public function setting($key, $default = null)
    {
        return data_get($this->settings, $key, $default);
    }

This I assume would be how I get the settings wherever I need them?

$resultsPerPage = Auth::user()->setting('results_per_page') ?? 15;

But what would be the best way to actually save/update these settings?

So I have my partial view

results-per-page.blade.php:

<form method="post" action="">
    <div class="input-field col s12">
        <select name="results_per_page">
            <option value="15">15</option>
            <option value="50">50</option>
            <option value="100">100</option>
            <option value="250">250</option>
            <option value="500">500</option>
            <option value="1000">1000</option>
        </select>
        <label>Results Per Page</label>
    </div>
</form>

Would I now make it a middleware that basically listens to POST requests and updates all the settings whenever a user makes a change, or also add it to the User class and explicitly write code in all controllers to update whatever settings the current view has?

Or is there an even better/easier solution I'm not seeing here :)

Thanks!

martinbean's avatar
Level 80

@madsem It’s not the job of a middleware. Middleware is for manipulating HTTP requests and responses. What you’re wanting to do is update an entity, which is the job of a controller.

I’d have a UserSettingsController that you can POST settings to, and it’ll update the current user’s settings. Something like:

Route::post('/user/settings', 'UserSettingsController@store');
class UserSettingsController extends Controller
{
    public function store(Request $request)
    {
        $request->user()->update([
            'settings' => $settings = array_merge(
                $request->user()->settings,
                $request->input('settings')
            ),
        ]);

        return response()->json($settings, 200);
    }
}
1 like

Please or to participate in this conversation.