laracasts/utilities
A collection of practical helpers and utilities from Laracasts for Laravel/PHP projects. Includes common convenience functions and small classes that streamline day-to-day development, reduce boilerplate, and support cleaner, more expressive code across your app.
This package enables seamless transfer of PHP data (like config, user info, flash messages) into JavaScript for Laravel apps. Start by installing via Composer (composer require laracasts/utilities). In modern Laravel (5.5+), the service provider auto-registers—no manual registration needed. For older versions, add Laracasts\Utilities\JavaScript\ServiceProvider::class to config/app.php. Optionally publish the config with php artisan vendor:publish --provider="Laracasts\Utilities\JavaScript\ServiceProvider" to customize encoding behavior. The simplest first use: in a controller or middleware, call JavaScript::put('key', $value) before returning a view, then output the data in Blade with @javascript inside a <script> tag.
PassVarsToJs) to inject per-request data like currentUser, flash messages, or locale settings—ensuring consistency across pages.@javascript after all JavaScript::put() calls. Wrap it in a <script> block to make the data available client-side:
<script>
var App = @javascript;
</script>
This binds the entire dataset to window.App for easy access in frontend code.auth()->user()?->only('id', 'name', 'email')) instead of full models or collections.JsonSerializable: Eloquent models and custom objects that implement JsonSerializable are automatically converted to JSON—ideal for API-like responses mixed into client-side state.put() must execute before @javascript renders. Middleware runs early and safely; putting logic in late-view composers or late in controller flow will fail silently or output empty data.</script>), avoid embedding unsanitized user input directly—JavaScript::put() serializes to JSON, but malicious scripts in strings may still execute if used in eval() or unsafe DOM insertion. Sanitize inputs or use e()/json_encode() manually for risky data.{{ dd((string) view('javascript')->render()) }} or inspect the HTML source to verify the generated JS matches expectations before debugging frontend side.config/javascript.php to tweak JSON options (e.g., JSON_PRETTY_PRINT for dev debugging, or JSON_INVALID_UTF8_IGNORE) or set a custom namespace (default: var laracasts = ...).How can I help you explore Laravel packages today?