简体   繁体   中英

Laravel 5.6 share auth id issue

I want to pass user id in the boot function of the AppServiceProvider class (as it written in the docs ) but for some reason Auth::user() object is empty.

myAppName/app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        URL::forceScheme('https');
        View::share('user_info', Auth::user());
    }

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

And then in in some blade template:

<p>{{ $user_info->id }}</p>

As a result an error occurs:

Trying to get property 'id' of non-object

There is same error if want to pass Auth::user()->id

How to make it work?

Laravel blade has access to the currently authenticated user, you don't have to explicitly use view::share for that.

You can simply access auth user in the blade as :

{{ Auth::user()->id }}

You might check if user already authenticated via @guest helper or auth check method:

@if (Auth::check())
  // authenticated
  {{ Auth::user()->id }}
@else
  // Not authenticaed
@endif

for security reason Laravel ServiceProvider doesn't have any access to session. And After login laravel maintain current login user into Session. That's why you are getting this error.

For your problem you can share variable into Controller(App/Http/Controllers/Controller.php) class's constructor.

Something like this:

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        view()->share('user',auth()->user());
    }
}

All controllers in Laravel default extends this class. Then you just have to execute constructor of parent class like this:

class HomeController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

One more thing default session is not sharable into Constructor also. You have to enable it by adding this two lines into App/Http/Kernel.php class's $middleware array.

class Kernel extends HttpKernel
{
    protected $middleware = [
       //
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
       //
    ];
}

Make sure you are removing this two lines from $middlewareGroups array. Good luck !!!

This is a solution, just need to add View Composer to the AppServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\URL;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.navbar', function($view) {

           $view->with('user_id', Auth::user()->id);

        });
    }

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

Then just put a variable in the navbar template like this: <p>{{ $user_id }}</p>

Explanation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM