nemke/select2js-bundle
A Symfony bundle that integrates Select2.js, the jQuery-based replacement for select boxes, providing searchable, taggable, and AJAX-powered dropdowns through Twig helpers and bundle configuration.
This package ships front-end components for Laravel applications; release 1.02 bundles Select2 4.0.1. Treat it as an asset-provider plus a thin Blade integration layer rather than a full form library.
Install and publish assets
composer require <vendor>/<package>
php artisan vendor:publish --provider="Vendor\Package\ServiceProvider" --tag=assets
Check config/<package>.php after publishing — it typically controls the asset path, CDN vs. local delivery, and whether the component auto-registers a Blade directive.
Look here first
config/<package>.php — asset source and any default Select2 options.resources/views/vendor/<package>/ — the Blade partials/directives actually rendered.public/vendor/<package>/ — the published select2.min.js / select2.min.css (verify the version string in the file header says 4.0.1).First use case
The fastest win is a searchable <select> on a create/edit form:
@push('styles')
<link rel="stylesheet" href="{{ asset('vendor/<package>/select2.min.css') }}">
@endpush
<select name="user_id" class="form-control select2">
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
@push('scripts')
<script src="{{ asset('vendor/<package>/select2.min.js') }}"></script>
<script>
$(function () { $('.select2').select2(); });
</script>
@endpush
If the package registers a directive, prefer @select2(['name' => 'user_id', 'options' => $users]) over hand-rolling the markup — it keeps option formatting consistent across the app.
Asset delivery via stacks, not inline tags. Keep <link> / <script> in @push('styles') and @push('scripts') so layouts control ordering. Select2 4.0.1 is jQuery-dependent, so its <script> must come after jQuery in the stack — this is the single most common integration failure.
Reusable wrapper partial. Add one partial (e.g. partials/select2.blade.php) that renders the <select>, a hidden fallback, and the init script keyed off a data-* attribute. Call it from every form instead of repeating JS:
<select name="{{ $name }}" class="form-control select2-remote"
data-url="{{ route('api.users.search') }}"
data-placeholder="Search users…">
</select>
AJAX-backed selects. For large tables, point Select2 at a JSON endpoint returning { results: [{ id, text }], pagination: { more: true } } (Select2 4.x format — note this differs from 3.x's id/text top-level shape). Guard the route with auth middleware; these endpoints get hammered on keystroke.
Validation round-trips. Select2 hides the native <select>, so Laravel's old() repopulation still works only if the underlying option exists. For remote selects, re-render the selected option server-side on validation failure, otherwise the field silently blanks and users resubmit.
Livewire / Alpine integration. Re-initialize after DOM swaps:
document.addEventListener('livewire:load', () => {
Livewire.hook('message.processed', () => $('.select2').select2());
});
Initialize on Alpine's x-init and destroy() on teardown to avoid duplicate widget instances.
Multiple instances and dynamic rows. If you clone form rows (invoice lines, permission matrices), call .select2({...}) on the new element and .select2('destroy') before removing the old one. Orphaned Select2 containers leak DOM nodes quickly in long-lived edit screens.
$.trim removal and event delegation under newer jQuery. If you're on jQuery 3.5+, smoke-test before relying on it; consider whether a maintained fork is acceptable to your team..select2-container .select2-selection--single { height: 38px; } etc.). Budget for it — it's not a one-liner in BS5.$(...).select2 is not a function almost always means the script tag loaded before jQuery or the asset wasn't published. Check the rendered page source, not the Blade file.php artisan view:clear and config:clear after upgrading to 1.02 — stale compiled views are a frequent source of "the new version isn't loading."select2.min.js contains 4.0.1 in its header comment; upgrading the composer package does not overwrite already-published files unless you re-run vendor:publish --force.dropdownParent: $('#myModal') in the options — required, not optional.required server-side validation is fine, but client-side HTML5 validation won't flag a visually empty Select2. Pair with a JS check or rely on server-side @error display.resources/views/vendor/<package>/. Override the view rather than patching published assets, so future upgrades don't clobber your changes.How can I help you explore Laravel packages today?