Skip to content
Advertisement

Laravel: Drop down options pulled from database in Master blade template are not showing on other pages

I have created a menu in master blade template in Laravel and have extended that to the other pages. That menu has a dropdown, with several options, that get fetched from the database. While, this works on the master template. It throws the same error on rest of the pages.

How should I fix this?

Here’s the error:

"ErrorException
Undefined variable: types (View: /Applications/MAMP/htdocs/bootstrap/laravel-project.app/resources/views/layouts/master.blade.php)"

Here’s the web.php code:

Route::get('/master', [MenuController::class, 'show'])
    ->name('master.show');

Controller.php

    <?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsDoshaType;

class MenuController extends Controller
{
    public function show(){
           
        $types = DoshaType::all();
    
        return view('layouts/master', compact('types'));
        
    }

}

view code

<!doctype html>

<html>

    <head>
        
        
    </head>


    <body>

        <div class = "container-fluid">
            
            <div class = "row">
                <div class = "col-2 mt-2">
                    <div class = "logo">
                        <img src = "./images/logo.png" id= "logo">
                    </div>
                </div>
                
                <div class = "col-10">

                    <nav class="navbar navbar-expand-lg navbar-dark bg-primary mt-2">
                        

                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        
                        <div class="collapse navbar-collapse" id="navbarNavDropdown">
                            <ul class="navbar-nav">
                            <li class="nav-item active">
                                <a class="nav-link" href="home">Home <span class="sr-only">(current)</span></a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">A</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="#">Y</a>
                            </li>
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                
                                M
                                
                                </a>
                                
                          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                                    @foreach ($types as $type) 
                                           
                                        <a class="dropdown-item" href="#">{{ $type->dosha }} </a>
                                    @endforeach
                                </div>

Advertisement

Answer

You need to create a global function for that because every page doesn’t have $type data. create the function in the helper file.

function types(){
    $types = DoshaType::all();
    return $type;
}

Now in the master file call this function.

@php $types = types(); @endphp
@foreach ($types as $type)                                        
    <a class="dropdown-item" href="#">{{ $type->dosha }} </a>
@endforeach

Another way make this query in your blade file

@php $types = AppDoshaType::all(); @endphp
@foreach ($types as $type)                                        
    <a class="dropdown-item" href="#">{{ $type->dosha }} </a>
@endforeach
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement