components/jquery-cookie
Lightweight jQuery plugin to read, write, and delete browser cookies. Supports session and expiring cookies, path/domain/secure options, and listing all cookies. Works with AMD/CommonJS loaders; include after jQuery.
Include jQuery and the plugin in your Laravel project’s assets:
<!-- resources/views/layouts/app.blade.php -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{{ asset('js/jquery.cookie.js') }}"></script>
jquery.cookie.js in public/js/ and reference it via asset().First use case: Set a session cookie
// resources/js/app.js
$(document).ready(function() {
$.cookie('user_prefs', 'dark_mode', { expires: 30 }); // 30 days
});
Retrieve a cookie in Blade
@php
$prefs = json_decode($_COOKIE['user_prefs'] ?? '{}', true);
$theme = $prefs['theme'] ?? 'light';
@endphp
<div class="theme-{{ $theme }}">...</div>
User Preferences Storage
$.cookie('ui_settings', JSON.stringify({
fontSize: 14,
notifications: true
}), { expires: 365 });
$settings = json_decode($_COOKIE['ui_settings'] ?? '{}', true);
CSRF Token Sync (Legacy)
// Broadcast changes to other tabs
$(window).on('storage', function(e) {
if (e.key === 'XSRF-TOKEN') {
$.cookie('XSRF-TOKEN', e.newValue, { expires: 1 });
}
});
Conditional Cookie Logic
if ($.cookie('has_seen_tutorial')) {
$('#tutorial-modal').hide();
}
// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.copy('node_modules/jquery.cookie/jquery.cookie.js', 'public/js');
// Vue example
export default {
mounted() {
const token = $.cookie('auth_token');
if (token) this.fetchUserData(token);
}
};
public function handle($request, Closure $next) {
if (!isset($_COOKIE['required_cookie'])) {
abort(403);
}
return $next($request);
}
Deprecation Risk
js-cookie (modern, actively maintained).MIME-Type Issues
Laravel Session Conflicts
session() helper instead).JSON Serialization
// Bad: Large object
$.cookie('data', hugeObject);
// Good: Store IDs or hashes
$.cookie('data_id', 123);
console.log($.cookie('test')); // undefined if missing
$.removeCookie('debug_token');
// Dump all cookies in Laravel Tinker
dd($_COOKIE);
$.cookie('tenant_id', '123', {
path: '/',
domain: '.yourdomain.com'
});
Secure flag for HTTPS-only cookies:
$.cookie('api_token', 'abc123', {
secure: true,
sameSite: 'Strict' // Modern browsers
});
// app/Helpers/CookieHelper.php
class CookieHelper {
public static function get($name) {
return $_COOKIE[$name] ?? null;
}
}
How can I help you explore Laravel packages today?