Peter Peter - 5 months ago 25
PHP Question

Undefined variable in view on server

My Laravel webpage works perfectly fine locally, but when I upload it to my server it breaks. It was working fine until the last update I made, where I added more or less the entire webpage.

There are some version mismatches between the two installations:

Local: Arch Linux

Server: Ubuntu 14.04

Local: php 5.6.16

Server: php 5.5.9

Here is a screenshot of the error I am currently facing, but there are more. If I comment this line another error shows:
Error
The failing view is a bar which is shown on all pages except the login page. "layouts/topbar.blade.php":



<div class="container topbar-container" style="text-align:right;">
<a class="btn btn-primary active" role="button" href= {{URL::to('/dashboard')}}>Dashboard</a>
@if ($role == "admin")
<a class="btn btn-primary active" role="button" href= {{URL::to('/dashboard/admin_settings')}}>Admin Settings</a>
@endif
<a class="btn btn-primary active" role="button" href= {{URL::to('/dashboard/settings')}}>Settings</a>
<a class="btn btn-primary active" role="button" href="{{ URL::to('/auth/logout') }}">Sign Out</a>
</div>





The view is included in the "layouts/master.blade.php" file:



@if(!Request::is('auth/login'))
<!! sidebar not included on login page. >
@include('layouts.topbar')
@endif





Using this View Composer "ViewComposers/UserComposer.php":

<?php

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;
use Auth;

class UserComposer
{
/**
* Create a new profile composer.
*
* @param UserRepository $users
* @return void
*/
public function __construct()
{
}

/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$view->with('role', Auth::user()->role);
}
}


I am happy to provide more information if necessary, but I don't know what is relevant to be honest.

Answer

Alright just do this , its easy so lets do it from scratch

in your root directory open your terminal and type

php artisan make:provider ViewcomposerProvider

then navigate and open app\providers\ViewcomposerProvider.php

add this code to that file

    namespace App\Providers;

use Auth;
use Illuminate\Support\ServiceProvider;

class Viewcomposer extends ServiceProvider {
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot() {
        view()->composer(['layouts.topbar'], function ($view) {

            if (Auth::check()) {
                $view->with('role', Auth::user()->role);
            } else {
                abort(403, 'Unauthorized action.');
            }
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register() {
        //
    }
}

Then just go to config\app.php

and add this to the providers array

    App\Providers\ViewcomposerProvider::class,

This should work fine (tested on Ubuntu env)

EDIT

change the way you include topbar like this

     @if(!Request::is('auth/login') && Auth::check())
       @include('layouts.topbar')
     @endif

EDIT 2 Try this in your topbar (in this case i think there is no need for Viewcomposer but it should fix the issue)

   @if (Auth::user()->role == "admin")
        <a class="btn btn-primary active" role="button" href= {{URL::to('/dashboard/admin_settings')}}>Admin Settings</a>
    @endif