我想访问模型中当前登录的用户ID。所以在模型中,我已经编写了代码;
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Auth;在模型下的方法中,我使用了以下代码:Auth::user()->id,但是,我收到了错误:
尝试获取非对象的属性的
发布于 2018-05-28 18:40:55
首先,您需要检查是否存在登录的用户,然后检查id
if (Auth::check()) {
//there is a user logged in, now to get the id
Auth::user()->id
}发布于 2018-05-28 18:36:00
要检索id,您必须使用:
Auth::id()可以像您尝试的那样访问其他用户属性
Auth::user()->otherProperties发布于 2018-05-28 19:43:36
use Illuminate\Support\Facades\Auth;//获取当前通过身份验证的用户...
$user = Auth::user();//获取当前通过身份验证的用户ID...
$id = Auth::id();https://stackoverflow.com/questions/50564362
复制相似问题