How to Install Bootstrap in Laravel 8

How to install bootstrap in laravel 8

You can install bootstrap manually and compile sass.

First, add it to npm with compilers:

npm install bootstrap
npm install sass
npm install sass-loader

Second, create (if not created) resources/sass/app.scss and add this:

@import '~bootstrap';

Third, add compilation in webpack.mix.js:

mix.sass('resources/sass/app.scss', 'public/css')

Than, compile it with npm run dev - you see compiled file public/css/app.css

Now you can use it in templates with asset:

<link href="{{ asset('css/app.css') }}" rel="stylesheet">

How to add Bootstrap, jQuery, and popper.js to Laravel 8 project using laravel-mix?

I had a lot of problems with using laravel/ui to install and then use the packages. Also the bootstrap version it installs is outdated as it is only version 4.6, and the newest version is, as of today: 5.1.1 (https://github.com/twbs/bootstrap/releases), so i decided to try install the packages by myself, and finally got everything working.

What I did was to install all the packages with the following commands:

#We do this because it will take care of a lot of things for us
composer require laravel/ui
php artisan ui bootstrap

#And now we upgrade bootstrap and add new popper.js version
npm install bootstrap@latest @popperjs/core --save-dev

This should install all the node_modules including bootstrap v. 5.1.1, jquery v. 3.6, and @popperjs/core v. 2.10.2 and package.json now looks like this:

{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "mix",
"watch": "mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"prod": "npm run production",
"production": "mix --production"
},
"devDependencies": {
"@popperjs/core": "^2.10.2",
"axios": "^0.21",
"bootstrap": "^5.1.1",
"jquery": "^3.6",
"laravel-mix": "^6.0.6",
"lodash": "^4.17.19",
"popper.js": "^1.16.1",
"postcss": "^8.1.14",
"sass": "^1.32.11",
"sass-loader": "^11.0.1"
}
}

Now in resources/js/bootstrap.js I change the code so it now looks like this:

window._ = require('lodash');

/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/

try {
window.$ = window.jQuery = require('jquery');
window.Popper = require('@popperjs/core');
window.bootstrap = require('bootstrap');

} catch (e) {}

/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

After this change, just run

npm run dev

Now everything works. Here is an example HTML/JS page that puts them all to the test

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>jQuery, popper.js, and Bootstrap</title>

{{-- Load compiled CSS --}}
<link rel="stylesheet" href={{ asset('css/app.css') }}>

{{-- popper.js CSS example --}}
<style>
#tooltip {
background: #333;
color: white;
font-weight: bold;
padding: 4px 8px;
font-size: 13px;
border-radius: 4px;
}
</style>
</head>
<body>

{{-- Test Bootstrap css --}}
<div class="alert alert-success mt-5" role="alert">
Boostrap 5 is working using laravel 8 mix!
</div>

{{-- popper.js HTML example --}}
<button id="button" aria-describedby="tooltip">My button</button>
<div id="tooltip" role="tooltip">My tooltip</div>

{{-- Load compiled Javascript --}}
<script src="{{ asset('js/app.js') }}"></script>
<script>
//Test jQuery
$(document).ready(function () {
console.log('jQuery works!');

//Test bootstrap Javascript
console.log(bootstrap.Tooltip.VERSION);
});

//Test popper.js
const button = document.querySelector('#button');
const tooltip = document.querySelector('#tooltip');
const popperInstance = Popper.createPopper(button, tooltip);
</script>

</body>
</html>

How to properly start Laravel 8 with Bootstrap & authentication

The steps are correct. For every new Laravel 8.x project that will include Bootstrap and auth scaffolding, you want to run the following.

composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run dev

Make a note of the generated /resources/views/layouts/app.blade.php. You will want to include this in all of your views.

@extends('layouts.app')

Unable to install Bootstrap 5.0.1 package in Laravel 8

I found a video tutorial solving (almost totally) my question (Spanish): https://www.youtube.com/watch?v=MXLiTcNIK2Q

Basically he installs bootstrap via ui. That installs v4.x.x as described in my question. Then he uses the following line: npm install bootstrap@next, which he gets from official Bootstrap website, despite I can't find it there anymore. The '@next' seems to do the trick but no clue about it, sorry.

That installed in my Laravel project bootstrap 5.0.0-beta3 (instead of latest 5.0.1, no clue either). Updating popper is what finally makes it all work.

In the video you can see how to install bootstrap --auth, too. But that was not part of my original question.

Despite I still don't know how to get to latest Bootstrap 5 version it solved my project so far. Any clues about that and about the '@next' bit are welcome.

Have a nice day!

How do I add popper.js to a Laravel 8 with Bootstrap 5 project?

1 - Install @popperjs/core using this command: npm i @popperjs/core

2 - Add this line to your bootstrap.js file:

window.Popper = require('@popperjs/core');

3 - Run this command: npm run dev

4 - Now you have access to window.Popper in your blade files.



Related Topics



Leave a reply



Submit