Laravel设置了全局变量

问题描述 投票:1回答:1

我如何在Laravel中设置全局变量,我在下面尝试但是它没有按预期工作。

我在config中创建了常量文件。

constants.php

return
[
'companyID' => 'Auth::user()->company',
]

user.php的

public static function TEST()
{
 return USER::Where('company',config('constants.companyID'))->get();
 }
laravel global-variables global
1个回答
0
投票

你不能在配置文件中使用auth()->user()。此外,这种方法是不对的。由于auth()->user()是一个全局对象,只需使用此查询:

public static function test()
{
    return User::where('company', auth()->user()->company)->get();
}

您可以从任何类或任何视图访问auth()->user()->company

© www.soinside.com 2019 - 2024. All rights reserved.