Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Select2Js Bundle Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

  1. 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.

  2. 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).
  3. 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.

Implementation Patterns

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.

Gotchas and Tips

  • Select2 4.0.1 is a 2016-era build. It predates several jQuery 3.5+ behavioural changes and has known issues with $.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.
  • Theme mismatch. 4.0.1 ships no Bootstrap 4/5 theme. Bootstrap 3 users get close-to-native styling; Bootstrap 4/5 users will see a mismatched control height and focus ring and need custom CSS (.select2-container .select2-selection--single { height: 38px; } etc.). Budget for it — it's not a one-liner in BS5.
  • CSS/JS ordering bugs look like JS errors. $(...).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.
  • Cache-busting. If the package registers assets through a manifest or versioned URL, clear 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."
  • Verify the published version. There's no lockfile for published public assets. Confirm 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.
  • Modals. Select2 dropdowns inside Bootstrap modals clip or render behind the overlay. Fix with dropdownParent: $('#myModal') in the options — required, not optional.
  • Validation messages. Hidden native selects still submit, so 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.
  • Extension points. Most packages of this shape expose a config array merged into the JS init and a publishable view you can override in resources/views/vendor/<package>/. Override the view rather than patching published assets, so future upgrades don't clobber your changes.
  • Debugging tip. When behaviour differs between environments, compare the published asset version and the config cache state first — with bundled front-end components, environment drift is nearly always asset drift, not application code.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle
adachsoft/api-version-bundle
elsayed85/laravel-api-exceptions
ouhssini/laravel-demo-mode
daworks/ncloud-outbound-mailer-driver
gp247/cms
ignibyte/aic-gates
nexa-merchant/blog
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
cgv/localized-resource