ronrun
ronrun

Reputation: 1234

Laravel 5.5, Auth::user() not working in Controller.php

I follow the video Multiple Authentication in Laravel 5.4 Natively! (Admins + Users) - Part 1
From part1 to 5, it's all done. Now I can login, logout.
Buy when I want to get the admin's properties, I got problem.

In admin blade, I can use {{ Auth::user()->name }}, it's ok.
In Composer, it's also ok.

<?php

namespace App\Http\ViewComposers;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Auth;

class AdminComposer
{
  /**
   * Create a movie composer.
   *
   * @return void
   */
  public function __construct(Request $request)
  {
    $this->admin= Auth::user();
    $this->base = config('app.url') . '/admin/';

  }

  /**
   * Bind data to the view.
   *
   * @param  View  $view
   * @return void
   */
  public function compose(View $view)
  {
    $view->with('admin', $this->admin);
    $view->with('base', $this->base);
  }
}

Then I try to use View in Controller, there is nothing, and no error message.

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Auth;

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

  public function __construct()
  {
    $admin = Auth::user();
    echo "<pre>", print_r($admin, 1), "</pre>"; exit;
    if(!empty($admin)){
      View::share('admin', $admin);
    }
  }
}

Why Auth::user() is ok in blades and composer, not working in controller ?


/config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\Admin::class,
    ],
],

Maybe I found the reason. Though still not resolved.

In laravel's Controller.php, it's not working. But in any other controller I created, it's ok

For example, the following is ok.
App\Http\Controllers\Admin\HomeController.php

public function __construct()
{
  $this->middleware('auth:admin');
  parent::__construct();
}

public function index()
{
  $admin = Auth::user();
  dd(Auth::user());
}

Maybe this is because the middleware.

In other post, someone uses Auth::guard('mrm')->User()
But I do the same, I tried Auth::guard('admin')->User()
Still nothing...

Upvotes: 1

Views: 9010

Answers (2)

parker_codes
parker_codes

Reputation: 3397

I typically use Illuminate\Support\Facades\Auth as the pulled in namespace. Try that.

Upvotes: 1

DevK
DevK

Reputation: 9942

Since Laravel 5.3 you are no longer able to access session in controller constructors, because middleware has not run yet. Authenticating a user, or getting the authenticated user requires session middleware to have already run.

You can define a closure (scroll to "Session In The Constructor") that happens after the session middleware has run.

Upvotes: 4

Related Questions