我目前正在 FilamnentPHP v3 和 Laravel 11 中开发一个项目,我试图弄清楚如何将当前 user_id 作为我的 AdminPanelProvider 中的参数传递给同一类中的函数。
我的 AdminPanelProvider 类
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
->brandName(fn() => auth()->user()->name ?? 'Reporting System') // auth here in brandName is working fine
->brandLogo(asset('logo.png'))
->brandLogoHeight('4rem')
->colors([
'primary' => Color::Red,
])
->sidebarCollapsibleOnDesktop()
->navigationItems(self::NavigationReportList(fn()=>auth()->user()->id)) // here is my problem
// rest of Panel function code
.....
}
public static function NavigationReportList($userId)
{
dd($userId,Auth()->id(),fn()=>Auth()->id()); // can't get auth id with the three options
[The dd data that i get when trying to get the closure auth id attached below]
$reports = Report::whereHas('users', function ($query) {
$query->where('users.id', auth()->id());
})
->select('id')
->orderBy('id', 'desc')
->limit(4)
->pluck('id')
->toArray();
// rest of my code
.....
}
所以我想要的就是在静态函数 NavigationReportList 中获取身份验证 ID
任何帮助将不胜感激
auth id dd 的图片https://i.sstatic.net/7A8JyxCe.jpg
尝试使用
->navigationItems(self::NavigationReportList(auth()->user()->id))
您必须将
id
作为参数传递。当您使用 fn() => auth()->id()
时,函数将作为参数传递,而不是 id
。在brandName
中,它之所以有效,是因为它是一个灯丝函数,并且灯丝可以处理闭包。