Jay Patel
Jay Patel

Reputation: 33

Trying to share data across views Laravel

I am trying to share the username across my fixed blade which I am extending in other views. I am using the controller to verify my users and thereby passing the data to the views. However, I get an error saying

Undefined variable: username (View: 
E:\LaravelProjects\adminLTE\resources\views\layout\fixed.blade.php) 

I have clearly passed the data using the View:share in my controller. What could be my possible error?

My code is as follows:

controller.php

    <?php

namespace App\Http\Controllers;

use DB;
use session;
use App\User;
use Carbon\Carbon;
use Auth;
use View;
use Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input; 
use Redirect;
use Illuminate\Http\Request;

class mainController extends Controller
{   
    //user login
    public function login(){
        return view('login');
    } 

    public function logs_in(Request $request){ 
        $email = $request->input('email');
        $password = $request->input('password');

        $hashedPassword = User::where('email', $email)->first();


        $role_type = User::select('role_type')->where('email', $email)->get();
        $username = User::select('name')->where('email', $email)->get();

            if(Hash::check($password, $hashedPassword->password)){ 
                if($role_type == '[{"role_type":"Administrator"}]'){ 
                    $request->session()->put('admin_name', $username );
                    View::share('username', $email);
                    return redirect()->route('dashboard');
                } else if ($role_type == '[{"role_type":"Staff"}]') {
                    $request->session()->put('success');
                    return redirect()->route('staff');
                } else if ($role_type == '[{"role_type":"User"}]') {
                    $request->session()->put('success');
                    return redirect()->route('user_dashboard');
                };
            } else {
                return redirect()->route('login')->with('login_error', 'Invalid 
                credentials entered');
            };
    }

fixed.blade.php I am getting the error from this view

 <li class="dropdown user user-menu">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
      <img src="{{ asset ('images/logo.png')}}" class="user-image" alt="User Image">
      <span class="hidden-xs"><i>{{ $username }}</i></span>
    </a>
    <ul class="dropdown-menu">
      <!-- User image -->
      <li class="user-panel">
        <p align="center">
          <img src="{{ asset ('images/logo.png')}}" style="width:100px;height:100px;" alt="User Image" >
          <h4 align="center">{{ $username }}</h4>
        </p>
      </li>

Upvotes: 2

Views: 1485

Answers (3)

Mehravish Temkar
Mehravish Temkar

Reputation: 4365

If you use attempt method of laravel for login as specified here, you can access username and other fields of the user that is logged in anywhere without the need to share the variables accross views.

{{ auth()->user()->username }}

Upvotes: 1

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

in Your AppServiceProvider

public function boot()
{
    View::share('username', request()->input('email'));
}

Now this value will be shared across all the views and you can access

{{ $username }}

if you want to access logged in user data you can use

@if(auth()->check())
  {{ auth()->user()->email }}
@endif

Hope this helps

Upvotes: 1

Mahdi Younesi
Mahdi Younesi

Reputation: 7489

From the docs

you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:

<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
   public function boot()
   {
       View::share('key', 'value'); //Do the sharing here
   }

/**
 * Register the service provider.
 *
 * @return void
 */
   public function register()
   {
      //
   }
}

Upvotes: 0

Related Questions