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');
}
}我又犯了同样的错误。
发布于 2021-10-08 01:05:26
由于用户未经过身份验证而出现的错误。可能在调用刀片文件中的组件之前添加一个检查,将组件包装在@auth指令之间。就像这样
@auth
<x-navbar></x-navbar>
@endauthhttps://stackoverflow.com/questions/69489306
复制相似问题