[英]“Trying to get property 'id' of non-object” Laravel - Auth::id() command
This is a sample of my code from DatatablesController.php这是我来自 DatatablesController.php 的代码示例
$id = Auth::user()->id;
$data = Cdr::
select('src','dst','start','duration','billsec','disposition','uniqueid')
->where('billsec','>', '0')
->where('start','>=',Carbon::today())
->join('alt_portal.numbers', 'nr','=','dst')
->where('alt_portal.numbers.user_id','=',$id)
->get();
I am doing Laravel project in which will be a lot of users.我正在做 Laravel 项目,其中会有很多用户。 And each user has it's own number or numbers.每个用户都有自己的号码。 Basically, I am trying to select from database only those numbers which belong to specific user, in other words, to logged in user's ID, because user can only see his/her outgoing and incoming calls in the table.基本上,我试图 select 从数据库中仅属于特定用户的那些号码,换句话说,登录用户的 ID,因为用户只能在表中看到他/她的呼出和呼入电话。
And when I am trying to do this:当我尝试这样做时:
$id = Auth::user()->id;
...
->where('alt_portal.numbers.user_id','=',$id)
I get a message in browser, which corresponds to an error:我在浏览器中收到一条消息,对应于错误:
message: "Trying to get property 'id' of non-object"消息:“试图获取非对象的属性 'id'”
Does anyone know how to correct this issue?有谁知道如何纠正这个问题?
PS I am new to Laravel. PS 我是 Laravel 的新手。
Case closed.结案。 Thank you guys for you responses.谢谢你们的回复。 I managed to fix my problem.我设法解决了我的问题。
I found solution here: ANSWER But I added stack directly to middleware, not in the 'web' group.我在这里找到了解决方案: ANSWER但是我直接将堆栈添加到中间件,而不是在“网络”组中。
This is my /Http/kernel.php file:这是我的 /Http/kernel.php 文件:
protected $middleware = [
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.