Noob Coder
Noob Coder

Reputation: 2917

Laravel 5.2 if else cond. in blade file not working

Im new in laravel.May i know why my if else conditions are not working? the problem is, even if admin is logged in, i dont see the admin panel button on this page and also the logout button. i only see the login button. i guess i have no logic errors on home.blade.php file. this problem is specific to this blade file only..other blade files with if else conditions are working perfectly. here are my blade files. im also sharing the github link of this project : https://github.com/TawsifKarim/jambura (its a basic CRUD application for product)so may be u can download run and check for the errors in my views/frontend/home.blade.php file

homelayout.blade.php

<html>
<head>
  <title>Product Management</title>
  <meta charset ="utf-8">
  <link rel="icon" type="png" href="img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" ></script>
    <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="{{ asset('css/style.css') }}">
    <script src="js/this.js"></script>
<body>

@yield('content')

</body>
</html>

home.blade.php

@extends('layout.homelayout')
@section('content')
<h2 id="Sitename">Product Management System</h2>

<img id="bannerimg" src="img/bg.jpg" class="img-responsive">

 <div class="container-fluid">
        <div class="row">
            <div class="col-md-2" id="homebutton">

                  <a href="/" class="btn btn-info btn-lg btn-block" id="Hbut">Home</a>
                  <a href="/Product" class="btn btn-info btn-lg btn-block" id="Hbut">Products</a>
                  <a href="#" class="btn btn-info btn-lg btn-block" id="Hbut">Contact us</a>
                  <a href="#" class="btn btn-info btn-lg btn-block" id="Hbut">About</a>

                  @if(!Auth::guest())
                  @if(Auth::user()->user_type_id == 1)
                  <a href="{{ url('/AdminPanel') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Admin Panel</a>
                  @endif
                  @endif

                  @if(Auth::guest())
                  <a href="{{ url('auth/login') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log in</a>
                  @else
                  <a href="{{ url('auth/logout') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log Out</a>
                  @endif

            </div>
            <div class="col-md-10">
                <div class="jumbotron">
                        <p id="startttl"> Laravel Project by Tawsif</p>
                        <p id="logindes">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex</p>
                </div>
            </div>
        </div>
      </div> 


@stop

Upvotes: 1

Views: 3111

Answers (3)

utsav
utsav

Reputation: 622

if else condition is working perfectly but Auth is using session and session is starting by default in laravel 5.1 with default middlewares.

protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,//here is starting of session code
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ];

but in laravel 5.2 middleware group concept is new which is added. laravel 5.2 predefined one middlware group like below.

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ]
];

so if you want use session or auth you have to add route group in routes like below

Route::group(['middleware' => ['web']], function () {
    //here you all routes
});

that' it.

Upvotes: 1

Noob Coder
Noob Coder

Reputation: 2917

after spending couple of hours working on this bug i have found that i have to put all the routes inside the web middleware in the routes.php file.. only the '/' route was outside of the middleware thats why it wasnt working. so now i made this->

   Route::get('/', function () {
    return view('frontend.home');
   });

into this

Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
        return view('frontend.home');
       });
});

and the bug is fixed :D

Upvotes: 0

smartrahat
smartrahat

Reputation: 5649

To check if any user is logged in or not, just simply use this

@if(Auth::user())
    // your code will be here
@endif

In your circumstances, you can use this

@if(Auth::user())
    <a href="{{ url('/AdminPanel') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Admin Panel</a>
    <a href="{{ url('auth/logout') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log Out</a>
@else
    <a href="{{ url('auth/login') }}" class="btn btn-info btn-lg btn-block" id="Hbut">Log in</a>
@endif

Upvotes: 0

Related Questions