我使用Laravel 5.3,我试图在constructor方法中获得authenticated用户的id,这样我就可以按指定的公司过滤用户,如下所示:

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 Illuminate\Support\Facades\View;
use App\Models\User;
use App\Models\Company;
use Illuminate\Support\Facades\Auth;


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

    public $user;
    public $company;


    public function __construct()
    {


        $companies = Company::pluck('name', 'id');
        $companies->prepend('Please select');
        view()->share('companies', $companies);
        $this->user = User::with('profile')->where('id', \Auth::id())->first();
        if(isset($this->user->company_id)){
            $this->company = Company::find($this->user->company_id);
            if (!isset($this->company)) {
                $this->company = new Company();
            }
            view()->share('company', $this->company);
            view()->share('user', $this->user);
        }

    }

但是,这不会返回用户id.我甚至试过Auth::check(),但都没用.

如果我将__construct()方法中的Auth::check()移出,则其工作原理如下:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        dd(\Auth::check());
        return view('home');
    }
}

然而,如果我把它也放在HomeController中的构造方法中,这是fails

Any ideas why this is failing?

推荐答案

docs

您无法访问服务器中的会话或经过身份验证的用户

As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:

class ProjectController extends Controller
{
    /**
     * All of the current user's projects.
     */
    protected $projects;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->projects;

            return $next($request);
        });
    }
}

Laravel相关问答推荐

在 Laravel 中排序的集合在 Vue 中突然不再排序

Laravel 获取具有多对多关系的中间表数据

使用 laravel 根据当月显示注册用户列表

基于值laravel提交表单后如何返回特定页面

在 laravel 5.3 中截断的错误日志(log)

Laravel,没有shell 访问的转储自动加载

Lumen 中的自定义 404 页面

Laravel Auth::attempt() 返回 false

我怎样才能了解更多关于我的 Laravel 排队作业(job)失败的原因?

Laravel timestamps() 不会创建 CURRENT_TIMESTAMP

Laravel 干预图像 GD 库扩展

eloquent的搜索/自定义属性的位置

使用数据库中的值进行动态邮件配置 [Laravel]

如何对模型执行删除操作?

如何在没有 Artisan 的情况下运行 Laravel?

如何访问 Carbon 对象类型?

Laravel 5 环境配置数组?

Laravel 5 - 判断日期是否是今天

Laravel/PHPUnit:断言 json 元素存在而不定义值

Laravel 5 默认注册后如何发送邮件?