简体   繁体   English

“试图获取非对象的属性 'id'” Laravel - Auth::id() 命令

[英]“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.

相关问题 Laravel:Auth::user()->id 试图获取非对象的属性 - Laravel: Auth::user()->id trying to get a property of a non-object 试图在控制器Laravel中获取非对象Auth :: user()-> id的属性“ id”? - Trying to get property 'id' of non-object Auth::user()->id in Controller Laravel? Laravel:试图为 Auth::User()->id 获取非对象的属性“id”; - Laravel: Trying to get property 'id' of non-object for Auth::User()->id; 在Laravel中使用Auth :: user()-> id摆脱“试图获取非对象的属性” - Get rid of “Trying to get property of non-object” with Auth::user()->id in Laravel 试图在laravel 5.4构造函数上获取非对象的属性Auth :: user()-> id - trying to get property of non-object on laravel 5.4 constructor Auth::user()->id Auth::user()->id 返回“试图获取非对象的属性”——Laravel 5.8 - Auth::user()->id returns "Trying to get property of non-object" -- Laravel 5.8 Auth :: user()-> id无法正常工作Laravel 5.2 /试图获取非对象的属性 - Auth::user()->id wont work Laravel 5.2 / Trying to get property of non-object LARAVEL 6.x = 试图获取非对象的属性“id” - LARAVEL 6.x = Trying to get property 'id' of non-object Laravel问题试图获取非对象的属性'id' - Laravel issue Trying to get property 'id' of non-object Laravel - 试图获取非对象的属性“id” - Laravel - Trying to get property 'id' of non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM