Laravel | Tipps und Tricks | Bootstrap verwenden

Create Laravel Project

Installing a fresh laravel project, so head over to the terminal, type the command, and create a new laravel app.

composer create-project laravel/laravel bootstrap

Step 2: Adding Bootstrap CDN

Using CDN it is very easy and simple for beginners.

You need to add Boostrap 5 CDN in before </head> tage add bootstrap.min.css cdn and before </body> add bootstrap.bundle.min.js

<!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>Boostrap 5</title>
    <link            
 href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
    rel="stylesheet"
    integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"     crossorigin="anonymous">
</head>
<body>
    <div class="container">
        <div class="alert alert-success mt-5" role="alert">
            Boostrap 5 is working!
        </div>    
    </div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
    crossorigin="anonymous">
</script>
</body>
</html>

Step 2:Adding Bootstrap Public Folder

Download Bootstrap 5 file and compress it, then we need to put file in public folder

After we can connect our style {{ asset(‚css/bootstrap.min.css‘) }} or js {{ asset(‚js/bootstrap.min.js‘) }}

<!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>Boostrap 5</title>
    <link rel="stylesheet" href={{ asset('css/bootstrap.min.css') }}>
</head>
<body>
    <div class="container">
        <div class="alert alert-success mt-5" role="alert">
            Boostrap 5 is working!
        </div>    
    </div>
    <script src="{{ asset('js/bootstrap.min.js') }}"></script>
</body>
</html>

Step 3: Adding Bootstrap 5 using Laravel Mix

In Laravel Mixing Bootstrap 5 Install is difficult than 1 and 2 method. you need to install first node npm if you do not have node npm package then install first other wise Laravel mix not working .

npm install

Next, you can see node_modules Folder it mean npm is working. Now we need to install bootstrap 5

npm install bootstrap --save-dev 

Next,we need run one more command npm install @popperjs/core –save-dev

npm install @popperjs/core --save-dev

After, you can see in package.json

"devDependencies": {
        "@popperjs/core": "^2.9.2",
        "axios": "^0.21",
        "bootstrap": "^5.0.0-beta3",
        "laravel-mix": "^6.0.6",
        "lodash": "^4.17.19",
        "postcss": "^8.1.14"
}

Next, you need to create folder scss inside app.scss file.resources/scss/app.scss put  @import „~bootstrap/scss/bootstrap“;

@import "bootstrap";

Next, For Js you need to add only resources/js/bootstrap.js put import „bootstrap“;

window._ = require("lodash");
import "bootstrap";

Then,run last command

npx mix

Then installed successfully Bootstrap 5 in laravel 8 .you can see we got new file app.css and app.js

let see

</head>
<body>
    <div class="container">
        <div class="alert alert-success mt-5" role="alert">
            Boostrap 5 is working using laravel 8 mix!
        </div>    
    </div>
    <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

de German
X