{{ auth()->user()->email }} / {{ Auth::user()->email }} 在 x-component 中不起作用
{{ auth()->user()->email }} / {{ Auth::user()->email }} not working in x-component
php artisan make:component Navbar
,创建时间:
App\View\Components\Navbar.php
app\resources\views\components\navbar.blade.php
在 blade 文件中放置 {{ 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 }}
添加到我的 blade 文件中,它起作用了。
但是,我想显示已验证用户的电子邮件地址,所以我将 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');
}
}
我又遇到了同样的错误。
您遇到的错误是因为用户未通过身份验证。也许在调用 blade 文件中的组件之前添加一个检查,将组件包装在 @auth 指令之间。像这样
@auth
<x-navbar></x-navbar>
@endauth
php artisan make:component Navbar
,创建时间:
App\View\Components\Navbar.php
app\resources\views\components\navbar.blade.php
在 blade 文件中放置 {{ 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 }}
添加到我的 blade 文件中,它起作用了。
但是,我想显示已验证用户的电子邮件地址,所以我将 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');
}
}
我又遇到了同样的错误。
您遇到的错误是因为用户未通过身份验证。也许在调用 blade 文件中的组件之前添加一个检查,将组件包装在 @auth 指令之间。像这样
@auth
<x-navbar></x-navbar>
@endauth