Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在尝试获取非对象Laravel 5.3、nav.blade.php的属性_Php_Laravel - Fatal编程技术网

正在尝试获取非对象Laravel 5.3、nav.blade.php的属性

正在尝试获取非对象Laravel 5.3、nav.blade.php的属性,php,laravel,Php,Laravel,我试图在不同的控制器中获取相同的视图文件nav.blade.php,但为管理员获取不同的内容,为用户获取不同的内容,但出现了一个错误 错误:View:/var/www/epharma/resources/views/nav.blade.php) 问题是这条线 第48行: @if(Auth::user()->type === 'User') <div> Html for user</div> @if(Auth::user()->type=='user') 面

我试图在不同的控制器中获取相同的视图文件nav.blade.php,但为管理员获取不同的内容,为用户获取不同的内容,但出现了一个错误

错误:View:/var/www/epharma/resources/views/nav.blade.php)

问题是这条线

第48行:

@if(Auth::user()->type === 'User')
    <div> Html for user</div>
@if(Auth::user()->type=='user')
面向用户的Html

当没有登录用户时,会发生这种情况。您可以这样做:

@if (Auth::guest())
  <p> Not logged in</p>

@elseif(Auth::user()->type === 'User')
  <p> Logged in</p>
@endif
@if(Auth::guest())
未登录

@elseif(Auth::user()->type=='user') 登录

@恩迪夫
当没有登录用户时,会发生这种情况。您可以这样做:

@if (Auth::guest())
  <p> Not logged in</p>

@elseif(Auth::user()->type === 'User')
  <p> Logged in</p>
@endif
@if(Auth::guest())
未登录

@elseif(Auth::user()->type=='user') 登录

@恩迪夫
试试这个

if(Auth::user() && Auth::user()->type === 'User')
如果第一个条件为false,它不会抛出错误

if(Auth::user() && Auth::user()->type === 'User')

如果第一个条件为false,则不会抛出错误

用户未经过身份验证,因此
Auth::user()
Null
。您正在尝试获取
null->id
。从非对象(
null
)获取id属性。
您必须首先检查用户是否通过
Auth::check()
Auth::user()!=空值
。等。然后尝试获取用户属性。

用户未经过身份验证,因此
Auth::user()
Null
。您正在尝试获取
null->id
。从非对象(
null
)获取id属性。
您必须首先检查用户是否通过
Auth::check()
Auth::user()!=空值
。等。然后尝试获取用户属性。

已经回答了很多问题,但您也可以这样做

@if (Auth::guest())
  <p> Not logged in</p>

@elseif(Auth::user()->type == 'User')
  <p> Logged in</p>
@endif
@if(Auth::guest())
未登录

@elseif(Auth::user()->type==“user”) 登录

@恩迪夫
已经得到了很好的回答,但您也可以这样做

@if (Auth::guest())
  <p> Not logged in</p>

@elseif(Auth::user()->type == 'User')
  <p> Logged in</p>
@endif
@if(Auth::guest())
未登录

@elseif(Auth::user()->type==“user”) 登录

@恩迪夫
请发布您的代码!请张贴您的密码!