Laravel 5要访问模型中的身份验证用户ID [英] Laravel 5 want to access auth user id in model
本文介绍了Laravel 5要访问模型中的身份验证用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想访问模型中当前登录的用户ID.因此,在模型中,我已经编写了代码;
I want to access the currently logged In user ID in the model. So in the model, I have written the codes;
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Auth;
在模型下的方法中,我使用了以下代码:Auth::user()->id
但是,我收到错误消息:
and in the method under the model, I used this code: Auth::user()->id
However, I receives the error:
试图获取非对象的属性
Trying to get property of non-object
推荐答案
首先,您需要检查是否存在登录的用户,然后检查ID
First you need to check if exists an user logged, then check the id
if (Auth::check()) {
//there is a user logged in, now to get the id
Auth::user()->id
}
这篇关于Laravel 5要访问模型中的身份验证用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文