Install the Package:
composer require components/bootstrap
This installs Bootstrap 4.x JavaScript files only (no CSS).
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).
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>×</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.
npm install bootstrap @popperjs/core jquery
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');
resources/js/app.js:
import 'bootstrap';
// Or manually:
// require('bootstrap');
npm install bootstrap @popperjs/core jquery
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,
}),
],
});
resources/js/app.js:
import 'bootstrap';
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>
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.
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>
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);
};
});
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);
});
});
Missing CSS:
bootstrap-default:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
jQuery Dependency:
<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>
Popper.js Missing:
bootstrap.bundle.min.js. If using separate files, ensure Popper is loaded.<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
Duplicate Initialization:
if (!window.bootstrapInitialized) {
new Bootstrap.Modal(document.getElementById('myModal'));
window.bootstrapInitialized = true;
}
Laravel Mix/Vite Caching:
npm run dev -- --clear
php artisan cache:clear
Check Console Errors:
F12) and look for errors like:
Bootstrap's JavaScript requires jQuery (missing jQuery).Cannot read property 'modal' of undefined (Bootstrap not loaded).Verify Asset Loading:
Network tab in dev tools. Look for:
bootstrap.bundle.min.js (200 OK status).Test Components Isolated:
Use Browser DevTools:
<button class="btn btn-primary"> <!-- Check for 'btn' and 'btn-primary' classes -->
Elements tab for unexpected styles overriding Bootstrap.How can I help you explore Laravel packages today?