Laravel 设置全局变量
Laravel set global variables
我如何在 Laravel 中设置全局变量,我在下面尝试过,但它没有按预期工作。
我在配置中创建了常量文件。
constants.php
return
[
'companyID' => 'Auth::user()->company',
]
user.php
public static function TEST()
{
return USER::Where('company',config('constants.companyID'))->get();
}
您不能在配置文件中使用 auth()->user()
。另外,方法也不对。由于 auth()->user()
是一个全局对象,只需使用此查询:
public static function test()
{
return User::where('company', auth()->user()->company)->get();
}
您可以从任何 class 或任何视图访问 auth()->user()->company
。
我如何在 Laravel 中设置全局变量,我在下面尝试过,但它没有按预期工作。
我在配置中创建了常量文件。
constants.php
return
[
'companyID' => 'Auth::user()->company',
]
user.php
public static function TEST()
{
return USER::Where('company',config('constants.companyID'))->get();
}
您不能在配置文件中使用 auth()->user()
。另外,方法也不对。由于 auth()->user()
是一个全局对象,只需使用此查询:
public static function test()
{
return User::where('company', auth()->user()->company)->get();
}
您可以从任何 class 或任何视图访问 auth()->user()->company
。