Friday, March 9, 2018

Trying to share data across views Laravel - development

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:LaravelProjectsadminLTEresourcesviewslayoutfixed.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 AppHttpControllers;

use DB;
use session;
use AppUser;
use CarbonCarbon;
use Auth;
use View;
use Hash;
use AppHttpControllersController;
use IlluminateSupportFacadesInput; 
use Redirect;
use IlluminateHttpRequest;

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="" class="user-image" alt="User Image">
      <span class="hidden-xs"><i></i></span>
    </a>
    <ul class="dropdown-menu">
      <!-- User image -->
      <li class="user-panel">
        <p align="center">
          <img src="" style="width:100px;height:100px;" alt="User Image" >
          <h4 align="center"></h4>
        </p>
      </li>



from Laravel Questions and Answers https://laravelquestions.com/php/trying-to-share-data-across-views-laravel/
via Lzo Media

No comments:

Post a Comment