birsoz
birsoz

Reputation: 7

Why Laravel bootstrap drowdown not working

here is my navbar

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="/teashirt/public/">TeaShirt</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="/teashirt/public/">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/teashirt/public/design">Desing your Teashirt</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/teashirt/public/about">About Us</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/teashirt/public/contact">Contact</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/teashirt/public/checkout">Checkout</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/teashirt/public/cart" tabindex="-1" aria-disabled="true">Cart</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Guest!</a>
                <div class="dropdown-menu" aria-labelledby="dropdown01">
                    <a class="dropdown-item" href="#">Profile</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Logout</a>
                </div>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
            <button class="btn btn-secondary my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>

Here is my bootsrap.js

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.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    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';

here is my app.js

require('./bootstrap');

Visual Studio Code offered to replace require to import, did that it didnt work.

I have tried -popper upgrade -composer update

Nothing is working. Everything works on the navbar but when i click dropdown it just goes to the link /#. I have tried it on IE and chrome. Same result

Upvotes: 0

Views: 1179

Answers (3)

NovelNet
NovelNet

Reputation: 11

I dont think the answer is fully solved as this solution is not really very acceptable. I could reproduce the error when doing a latest laravel 7 composer installation, followed with "artisan ui bootstrap" and npm install && npm run dev:

I get following error while using a collapse

Thats the value of var 'config'

Might be a webpack issue, as here it seems to be the same behavior: https://github.com/webpack/webpack/issues/10504

I havent found a solution yet - trying different laravel-mix, jquery and bootstrap versions.

Upvotes: 1

birsoz
birsoz

Reputation: 7

Adding this just before the `</body>` tagsolved my problem. 

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

I have tried it brfore too. But i think I have tried so many things that i cant tell why it didnt work before now it did.

Upvotes: -1

Mike Ross
Mike Ross

Reputation: 2972

Based on your files I assume that your problem could be one of the following

1) Sourcemaps in webpack.mix. Try following

const mix = require('laravel-mix');

mix.setPublicPath('public')
.setResourceRoot('../') // Turns assets paths in css relative to css 
.mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.sourceMaps();

2) Make sure you do not have any ad blockers enabled in your browser. It may block some of the js

3) Change your bootstrap.js to following

import _ from 'lodash';
import $ from 'jquery';
import 'popper.js'; // Required for BS4
import 'bootstrap';

window.$ = window.jQuery = $;
window._ = _; // Lodash

window.axios = require('axios');

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

I hope this helps. Let me know which solution worked for you.

Upvotes: 1

Related Questions