Laravel 6 及以上 laravel 版本如何声明一个变量用于所有函数?

How to declare a Variable to use for all function in Laravel 6 and above laravel version?

我要声明一个变量$id

此 ID 将是 Auth::id()

现在的方法是什么?

代码:

private static $id;

public function __construct()
{
   $this->id = Auth::id();
}

但是无法从函数查询访问此 ID。

Invoice::where([
     ['userID', '=', $id],
     ['created_at', '=', $todayDate]
])->get();

此行出错:['userID', '=', $id],

未定义变量 $id.

感谢提前..

public function __construct(){
    self::$id = Auth::id();
}

注意:如果 class 中有任何 static 成员函数或变量,我们不能使用 $this.

引用它

像下面这样使用

Invoice::where([
   ['userID'=>self::id],
   ['created_at'=>$todayDate]
])->get();
public function __construct()
{
   self::$id = Auth::id();
}
Invoice::where([
     ['userID', '=', self::$id],
     ['created_at', '=', $todayDate]
])->get();

使用 self 关键字,这会有帮助