尝试跨视图Laravel共享数据

时间:2018-03-09 06:59:21

标签: php laravel laravel-5

我正在尝试在我的固定刀片上共享用户名,我将在其他视图中进行扩展。我正在使用控制器来验证我的用户,从而将数据传递给视图。但是,我收到错误说

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

我已经使用我的控制器中的View:share清楚地传递了数据。什么可能是我可能的错误?

我的代码如下:

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 我从此视图中收到错误

 <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>

3 个答案:

答案 0 :(得分:1)

AppServiceProvider

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

现在,此值将在所有视图中共享,您可以访问

{{ $username }}

如果您想访问登录的用户数据,可以使用

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

希望这有帮助

答案 1 :(得分:1)

如果您使用attempt laravel方法登录指定here,则可以访问在任何地方登录的用户的用户名和其他字段,而无需共享视图中的变量。< / p>

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

答案 2 :(得分:0)

来自docs

  

您应该在服务提供商的启动方法中拨打电话进行共享。您可以自由地将它们添加到AppServiceProvider或生成一个单独的服务提供程序来容纳它们:

<?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()
   {
      //
   }
}