Php 如何声明用于Laravel 6及以上版本中所有函数的变量?

Php 如何声明用于Laravel 6及以上版本中所有函数的变量?,php,laravel,laravel-6,laravel-7,Php,Laravel,Laravel 6,Laravel 7,我想声明一个变量$id 此id将是Auth::id() 现在有什么办法 代码: 但无法从函数查询访问此id Invoice::where([ ['userID', '=', $id], ['created_at', '=', $todayDate] ])->get(); 此行中出现错误:['userID','=',$id] 未定义的变量$id 谢谢你的预付款 public function __construct(){ self::$id = Auth::id

我想声明一个变量$id

此id将是Auth::id()

现在有什么办法

代码:

但无法从函数查询访问此id

Invoice::where([
     ['userID', '=', $id],
     ['created_at', '=', $todayDate]
])->get();
此行中出现错误:['userID','=',$id]

未定义的变量$id

谢谢你的预付款

public function __construct(){
    self::$id = Auth::id();
}
注意:如果类中有任何
静态
成员函数或变量,我们不能使用
$this
引用它

就像下面这样使用

Invoice::where([
   ['userID'=>self::id],
   ['created_at'=>$todayDate]
])->get();
使用self关键字,这将有所帮助

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();