Laravel | Auth::user()->;id不是';t在AppServiceProvider中工作

Laravel | Auth::user()->;id不是';t在AppServiceProvider中工作,laravel,Laravel,当我将身份验证ID放入任何具有 Auth::user()->id 但当我把它放在AppServiceProvider.php中时,它返回“试图获取非对象的属性”id” 我不明白为什么 艾迪:我试过了,但还是不起作用 public function boot() { view()->composer('*', function ($view) { if (Auth::check()) { $id=Auth::user()->id; $idd=Perso

当我将身份验证ID放入任何具有

Auth::user()->id
但当我把它放在AppServiceProvider.php中时,它返回“试图获取非对象的属性”id”

我不明白为什么

艾迪:我试过了,但还是不起作用

public function boot()
{
    view()->composer('*', function ($view) 
{
if (Auth::check())
{
    $id=Auth::user()->id;
    $idd=Person::where('user_id','=',$id)->get('photo');

    $view->with('idd', $idd );
    $view->with('id', $id );
}
       });
}
错误:
传递给Illumb\Database\Grammar::columnize()的参数1必须是在

中调用的数组、给定字符串类型,若要获取当前经过身份验证的用户ID,请使用
Auth::id()

另一种情况可能是没有当前用户,在这种情况下,
Auth::user()
返回NULL。将代码包装在

if (Auth::check())
{
    // Do stuff
}
以确保有用户登录。

这是我的工作

<?php

namespace Fitness\Providers;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Request $request)
    {
    view()->share('user', $request->user());
    }
}

可能重复:您如何认证用户?可能重复Hello,感谢您的关注。我看到了重复的问题,我给你发了一个帖子。仍然不起作用。你能帮忙吗?编辑了答案,你有当前用户吗?我用一条新的错误消息再次编辑了帖子,你能检查一下吗?