Search code examples
phphtmllaravelauthenticationsidebar

Laravel: Change login layout error


I use the simple built-in php artisan make:auth authentication in laravel and I would like to change the default layout.

login.blade.php

@extends('layouts.default')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    <form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
                        {{ csrf_field() }}

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label for="email" class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                            <label for="password" class="col-md-4 control-label">Password</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-8 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Login
                                </button>

                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    Forgot Your Password?
                                </a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

default.blade.php

<!DOCTYPE html>
<html>
<head>
    @include('includes.head')
</head>

<body>

    @include('includes.sidebar') 

    <div id="main" class="row">
        <div class = "content">
            @yield('content')
        </div>

    </div>

    <script src="full_calendar/lib/jquery-ui.js"></script>
    <script src='full_calendar/lib/moment.min.js'></script>
    <script src='js/bootstrap.min.js'></script>
    <script src='js/sidebar.js'></script>

</body>
</html>

sidebar.blade.php

<nav class="navbar navbar-inverse sidebar" role="navigation">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-sidebar-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">{{ Auth::user()->name }}</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-sidebar-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/home">Home<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-home"></span></a></li>
                <li ><a href="{{ url('calendar_employee') }}">Calendar->employees<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></a></li>
                <li ><a href="{{ url('calendar_employer') }}">Calendar->employers<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></a></li>
                <li ><a href="{{ route('register') }}">Register<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-envelope"></span></a></li>
                <li ><a href="{{ route('logout') }}">Log out<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-envelope"></span></a></li>
                <li ><a href="#">About<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-logout"></span></a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Settings <span class="caret"></span><span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-cog"></span></a>
                    <ul class="dropdown-menu forAnimate" role="menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                        <li class="divider"></li>
                        <li><a href="#">One more separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

Error message

ErrorException in 23932780504d5d1cb3d219486de49ace0b7f23c9.php line 11: Trying to get property of non-object (View: C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php) (View: C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php) (View: C:\xampp\htdocs\laravel\resources\views\includes\sidebar.blade.php)

Why does this exception occur?


Solution

  • The issue in your code could be Auth::user()->name try dumping the value of Auth::user() (code is dd(Auth::user())) if it shows null that's the issue.

    and in below code

    <li ><a href="{{ url('calendar_employee') }}">Calendar->employees<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></a></li>
    <li ><a href="{{ url('calendar_employer') }}">Calendar->employers<span style="font-size:16px;" class="pull-right hidden-xs showopacity glyphicon glyphicon-user"></span></a></li>
    

    Instead of Calendar->employees you should write {{ $Calendar->employees }} and same for Calendar->employers instead you should write {{ $Calendar->employers }} without {{...}} or {!!...!!} it will not print the variable values.