Laravel更改col名称"user_id"会使$ user未定义?

TrO*_*nNe 1 php laravel

我的Laravel有一种奇怪的行为.

一切都工作得很好,但后来我决定将col名称"user_id"更改为"seller_id"

而且我也正确地更改了控制器中的列名称

public function transactions(){
    return $this->hasMany('App\Transaction', 'seller');
}
Run Code Online (Sandbox Code Playgroud)

然后我成功登录,并在一个视图中有这一行:

auth()->user->id
Run Code Online (Sandbox Code Playgroud)

返回我的错误:

"Undefined property:Illuminate\Auth\AuthManager :: $ user

我在互联网上看到我没有登录的错误和接缝,但我,任何线索?

谢谢

San*_*ari 8

您指出的错误与您的更改无关.

您可以在此处参考文档,以检索您可以调用的经过身份验证的用户:

// Get the currently authenticated user...
$user = Auth::user();
Run Code Online (Sandbox Code Playgroud)

要么

// Using helper function
$user = auth()->user()
Run Code Online (Sandbox Code Playgroud)

因此,要检索用户的ID:

$id = Auth::id();
Run Code Online (Sandbox Code Playgroud)

要么

$id = auth()->id();
Run Code Online (Sandbox Code Playgroud)

要么

$id = Auth::user()->id;
Run Code Online (Sandbox Code Playgroud)

要么

$id = auth()->user()->id;
Run Code Online (Sandbox Code Playgroud)

此外,您需要根据文档更改关系函数:

public function transactions(){
return $this->hasMany('App\Transactions', 'seller_id');
}
Run Code Online (Sandbox Code Playgroud)