【发布时间】:2021-11-28 00:37:48
【问题描述】:
php artisan make:component Navbar,已创建:
App\View\Components\Navbar.phpapp\resources\views\components\navbar.blade.php
将{{ auth()->user()->email }} 或{{ Auth::user()->email }} 放在刀片文件中,出现此错误:
-
Trying to get property 'email' of non-object。
尝试通过将我的App\View\Components\Navbar.php 更改为:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Navbar extends Component
{
public $email;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($email = null)
{
$this->email = 'info@example.com';
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.navbar');
}
}
并将{{ $email }} 添加到我的刀片文件中,它工作正常。
但是,我想显示来自经过身份验证的用户的电子邮件地址,所以我将App\View\Components\Navbar.php 更改为:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
use Illuminate\Support\Facades\Auth;
class Navbar extends Component
{
public $email;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($email = null)
{
$this->email = Auth::user()->email;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.navbar');
}
}
我又遇到了同样的错误。
【问题讨论】:
标签: laravel laravel-8 laravel-blade laravel-fortify