samedi 22 mai 2021

Undefined variable in app.blade.php laravel5.4

i want to list the name of courses from my database in my navbar menu (app view) :

    @foreach ($cours as $cour)
                <li><a class="dropdown-item"></a></li>
                @endforeach

and i created my layoutcontroller to display the view :

    class layoutController extends Controller
{
    public function showView()
    {
        $cours = DB::table('cours')->select('id','nom')->get();
        return view('app',['cours' => $cours]);
    }
    
   
}

then i made a route for it :

    Route::get('#','layoutController@showView');

the problem is when i navigate to login view for exemple it shows an error that the variable cours is undefined in my app layout view :

Undefined variable: cours (View: C:\wamp64\www\opencourses\resources\views\layouts\app.blade.php) (View: C:\wamp64\www\opencourses\resources\views\layouts\app.blade.php)

here is my login view :




@extends('layouts.app')
@section('content')


<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 offset-md-2 px-3 py-3 pt-md-4 pb-md-4">
            <div class="card">
                <div class="card-header">Connexion</div>
             
                <div class="card-body">
                    <form class="form-horizontal" method="POST">
                    
                    
                        
                        <div class="row mb-3">
                            <label for="email" class="col-sm-3 offset-sm-1 col-form-label">Adresse Email</label>
                            <div class="col-sm-7">
                              <input type="text" class="form-control" id="email" name="email">
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label for="password" class="col-sm-3 offset-sm-1 col-form-label">Mot de passe</label>
                            <div class="col-sm-7">
                              <input type="password" class="form-control" id="password" name="password">
                            </div>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="offset-sm-4 col-sm-7">
                              <button type="submit" class="btn btn-primary">Envoyer</button>
                            </div>
                        </div>
                    

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>


@endsection


and here is my app view :

<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    
    <title>Mon blog</title>
    <link rel="icon" href="https://www.jsdelivr.com/img/icon_256x256.png">

    

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css">
    
    <!-- other CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.3.1/css/flag-icon.min.css" rel="stylesheet"/>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- Bootstrap javascript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js"></script>
  </head>
  
  <body>
    <nav class="navbar navbar-expand-md navbar-light p-3 px-md-4 mb-3 bg-body border-bottom shadow-sm">
      <div class="container-fluid">
        <a class="navbar-brand" href="welcome.html">
            <img src="https://www.jsdelivr.com/img/icon_256x256.png" width="30" height="30" class="d-inline-block align-top" alt=""> Mon blog
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav ms-auto mb-2 mb-lg-0 d-flex">
            <li class="nav-item px-2">
              <a class="nav-link active" aria-current="page" href="<?php echo url('')?>">Accueil</a>
            </li>
            <li class="nav-item dropdown px-2">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                Articles
              </a>
              <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                @foreach ($cours as $cour)
                <li><a class="dropdown-item"></a></li>
                @endforeach
              </ul>
            </li>
            <!-- si utilisateur non authentifié -->
            @guest
            <li class="nav-item px-2">
                <a class="btn btn-outline-primary me-2" href="<?php echo url('login')?>">Connexion</a>
            </li>
            <li class="nav-item px-2">
                <a class="btn btn-primary" href="<?php echo url('register')?>">S'inscrire</a>
            </li>
            @else
            <!-- si utilisateur authentifié -->
            <li class="nav-item px-2">
                <a class="nav-link" href="home">Mon compte</a>
            </li>
            <li class="nav-item px-2">
                <a class="nav-link" href=""  onclick="event.preventDefault();
                document.getElementById('logout-form').submit();">Déconnexion</a>
            </li>
            @endguest
          </ul>
          <form id="logout-form" action="" method="POST" style="display: none;">
            
        </form>
        </div>
      </div>
    </nav>

    @yield('content')

    <footer class="container pt-4 my-md-5 pt-md-5 border-top">
        <div class="row">
          <div class="col-12 col-md">
            <img class="mb-2" src="https://www.jsdelivr.com/img/icon_256x256.png" alt="" width="24" height="24">
            <small class="d-block mb-3 text-muted">&copy; 2017–2021</small>
          </div>
          <div class="col-6 col-md">
            <h5>Features</h5>
            <ul class="list-unstyled text-small">
              <li><a class="link-secondary" href="#">Team feature</a></li>
              <li><a class="link-secondary" href="#">Stuff for developers</a></li>
              <li><a class="link-secondary" href="#">Another one</a></li>
              <li><a class="link-secondary" href="#">Last time</a></li>
            </ul>
          </div>
          <div class="col-6 col-md">
            <h5>Resources</h5>
            <ul class="list-unstyled text-small">
              <li><a class="link-secondary" href="#">Resource name</a></li>
              <li><a class="link-secondary" href="#">Another resource</a></li>
            </ul>
          </div>
          <div class="col-6 col-md">
            <h5>About</h5>
            <ul class="list-unstyled text-small">
              <li><a class="link-secondary" href="#">Privacy</a></li>
              <li><a class="link-secondary" href="#">Terms</a></li>
            </ul>
          </div>
        </div>
    </footer>


    
  </body>
</html>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire