Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 9 – How to display the number of my products from my shopping basket?

I’m creating a shop on Laravel 9 and I would like to add a shopping basket with the number of products I have added on my ‘paniers’ SQL table.

I wrote this on my controller :

    public function AffichageBoutique()
    {
        $boutiques = Boutique::all();
        $nbr = Panier::where('iduser', session('iduser'))->get()->count();
        return view('boutique.Boutique')->with('boutiques', $boutiques)->with('paniers', $nbr);
    }

$nbr is the number of my products I have on my basket, for example -> 4.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to display this number on my page like this, it’s an animated basket =>

            <div class="icon-wrapper">
            <figure>
                <img src="/img/icons/bell_icon.svg" alt="" class="bell-icon">
                <figcaption>
                    <p>{{ $nbr }}</p>
                </figcaption>
            </figure>
        </div>

But I have this error : Undefined variable $nbr.

What’s the problem ??
Thank you !

>Solution :

return view('boutique.Boutique')->with('boutiques', $boutiques)->with('paniers', $nbr);

in your return you are passing the $nbr variable and renaming it as 'paniers', either you change the name to 'nbr' (->with('nbr', $nbr)) or in your html you call for $paniers

You can find more in Laravel official documentation

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading