Weave Code
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

Bootstrap Laravel Package

components/bootstrap

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps for Laravel Integration

  1. Install the Package:

    composer require components/bootstrap
    

    This installs Bootstrap 4.x JavaScript files only (no CSS).

  2. Basic Blade Integration: Include the JS via CDN (quickest method):

    <!-- In your Blade template (e.g., `resources/views/layouts/app.blade.php`) -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    

    Or bundle it with Laravel Mix/Vite (recommended for production).

  3. First Use Case: Use a Bootstrap component (e.g., modal) in a Blade view:

    <!-- Button to trigger modal -->
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
        Launch Demo Modal
    </button>
    
    <!-- Modal markup -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Modal Title</h5>
                    <button type="button" class="close" data-bs-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Modal content goes here.
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                        Close
                    </button>
                </div>
            </div>
        </div>
    </div>
    

    Note: For styles, manually include Bootstrap CSS via CDN or use the components/bootstrap-default package.


Implementation Patterns

1. Asset Pipeline Integration

Laravel Mix (Webpack)

  1. Install Dependencies:
    npm install bootstrap @popperjs/core jquery
    
  2. Configure webpack.mix.js:
    const mix = require('laravel-mix');
    
    mix.js('resources/js/app.js', 'public/js')
        .postCss('resources/css/app.css', 'public/css', [])
        .copy('node_modules/bootstrap/dist/js/bootstrap.bundle.min.js', 'public/js/bootstrap.js');
    
  3. Import in resources/js/app.js:
    import 'bootstrap';
    // Or manually:
    // require('bootstrap');
    

Vite

  1. Install Dependencies:
    npm install bootstrap @popperjs/core jquery
    
  2. Configure vite.config.js:
    import { defineConfig } from 'vite';
    import laravel from 'laravel-vite-plugin';
    
    export default defineConfig({
        plugins: [
            laravel({
                input: ['resources/js/app.js'],
                refresh: true,
            }),
        ],
    });
    
  3. Import in resources/js/app.js:
    import 'bootstrap';
    

2. Dynamic Component Loading

Use lazy-loading for non-critical components to improve performance:

<!-- Load Bootstrap JS dynamically for a specific page -->
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const script = document.createElement('script');
        script.src = "https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js";
        script.defer = true;
        document.body.appendChild(script);
    });
</script>

3. Livewire/Alpine.js Integration

Livewire

Bootstrap JS works seamlessly with Livewire. Example:

// In a Livewire component
public function render()
{
    return view('livewire.example-component');
}
<!-- Blade template -->
<div wire:ignore>
    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#livewireModal">
        Open Modal
    </button>
    <div class="modal fade" id="livewireModal" tabindex="-1" role="dialog">
        <!-- Modal content -->
    </div>
</div>

Note: Use wire:ignore to prevent Livewire from managing the modal’s DOM.

Alpine.js

Bootstrap JS and Alpine.js can coexist. Example:

<div x-data="{ open: false }">
    <button @click="open = true" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#alpineModal">
        Open Modal
    </button>
    <div class="modal fade" id="alpineModal" tabindex="-1" role="dialog" x-show="open" @click.away="open = false">
        <!-- Modal content -->
    </div>
</div>

4. Customizing Bootstrap JS

Override Bootstrap’s default behavior by extending its JavaScript:

// In resources/js/bootstrap.js
document.addEventListener('DOMContentLoaded', function() {
    // Extend Bootstrap's modal behavior
    const originalShow = $.fn.modal.Constructor.prototype.show;
    $.fn.modal.Constructor.prototype.show = function() {
        // Custom logic before showing modal
        console.log('Custom modal logic');
        originalShow.call(this);
    };
});

5. Server-Side Rendering (SSR) with Inertia.js

For Inertia.js apps, ensure Bootstrap JS is loaded client-side:

// In your Vue/React app (e.g., `resources/js/app.js`)
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import Bootstrap from 'bootstrap';

createInertiaApp({
    resolve: (name) => require(`./Pages/${name}.vue`),
    setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) })
            .use(plugin)
            .mount(el);
    },
});

// Load Bootstrap JS globally
document.addEventListener('DOMContentLoaded', function() {
    // Initialize Bootstrap components
    const modals = document.querySelectorAll('[data-bs-toggle="modal"]');
    modals.forEach(modal => {
        new Bootstrap.Modal(modal);
    });
});

Gotchas and Tips

Pitfalls

  1. Missing CSS:

    • Issue: Bootstrap JS requires CSS for proper rendering. Without it, components will look broken or non-functional.
    • Fix: Always include Bootstrap CSS via CDN or bootstrap-default:
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
      
  2. jQuery Dependency:

    • Issue: Bootstrap 4.x requires jQuery. If jQuery is not loaded, JS components will fail.
    • Fix: Ensure jQuery is included before Bootstrap JS:
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
      
  3. Popper.js Missing:

    • Issue: Tooltips, popovers, and dropdowns require Popper.js, which is included in bootstrap.bundle.min.js. If using separate files, ensure Popper is loaded.
    • Fix: Use the bundle or include Popper manually:
      <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
      
  4. Duplicate Initialization:

    • Issue: Initializing Bootstrap components multiple times (e.g., in SPAs) can cause errors.
    • Fix: Use a flag to prevent re-initialization:
      if (!window.bootstrapInitialized) {
          new Bootstrap.Modal(document.getElementById('myModal'));
          window.bootstrapInitialized = true;
      }
      
  5. Laravel Mix/Vite Caching:

    • Issue: Cached assets may serve old versions of Bootstrap JS.
    • Fix: Clear caches after updates:
      npm run dev -- --clear
      php artisan cache:clear
      

Debugging Tips

  1. Check Console Errors:

    • Open browser dev tools (F12) and look for errors like:
      • Bootstrap's JavaScript requires jQuery (missing jQuery).
      • Cannot read property 'modal' of undefined (Bootstrap not loaded).
  2. Verify Asset Loading:

    • Ensure Bootstrap JS is loaded by checking the Network tab in dev tools. Look for:
      • bootstrap.bundle.min.js (200 OK status).
      • No 404 errors for jQuery/Popper.
  3. Test Components Isolated:

    • Create a minimal Blade template with just Bootstrap JS and a single component (e.g., modal) to rule out conflicts.
  4. Use Browser DevTools:

    • Inspect elements to ensure Bootstrap classes are applied:
      <button class="btn btn-primary"> <!-- Check for 'btn' and 'btn-primary' classes -->
      
    • Check the Elements tab for unexpected styles overriding Bootstrap.

Configuration Quirks

  1. **
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.
phalcon/cli-options-parser
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
directorytree/privacy-filter-classifier
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata