Skip to content

Instantly share code, notes, and snippets.

@SerendipityNL
Last active May 29, 2019 13:38
Show Gist options
  • Save SerendipityNL/6ca675bd8ff2711c69cb9c24d156b9ac to your computer and use it in GitHub Desktop.
Save SerendipityNL/6ca675bd8ff2711c69cb9c24d156b9ac to your computer and use it in GitHub Desktop.
Laravel AdminLTE Sidebar Toggle Cookie

This is Ardziej's solution:

JS

$.AdminLTESidebarTweak = {};

$.AdminLTESidebarTweak.options = {
    EnableRemember: true,
    NoTransitionAfterReload: false
    //Removes the transition after page reload.
};

$(function () {
    "use strict";

    function setCookie(value) {
        let name = 'toggleState';
        let days = 365;
        let d = new Date;
        d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
        document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
    }

    $("body").on("collapsed.pushMenu", function () {
        if ($.AdminLTESidebarTweak.options.EnableRemember) {
            setCookie('closed');
        }
    }).on("expanded.pushMenu", function () {
        if ($.AdminLTESidebarTweak.options.EnableRemember) {
            setCookie('opened');
        }
    });
});

PHP (/resources/views/vendor/adminlte/master.blade.php)

<body class="hold-transition @yield('body_class') @if (Cookie::get('toggleState') === 'closed') {{ 'sidebar-collapse' }} @endif">

Laravel Cookies Middleware

<?php

namespace App\Http\Middleware;

use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;

class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        'toggleState',
    ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment