首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >{{ auth()->user()->email }} /{ Auth::user()->email }不工作于x组件

{{ auth()->user()->email }} /{ Auth::user()->email }不工作于x组件
EN

Stack Overflow用户
提问于 2021-10-08 00:53:42
回答 1查看 165关注 0票数 0

php artisan make:component Navbar,创建:

  • App\View\Components\Navbar.php
  • app\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更改为:

代码语言:javascript
复制
<?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更改为:

代码语言:javascript
复制
<?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');
    }
}

我又犯了同样的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-08 01:05:26

由于用户未经过身份验证而出现的错误。可能在调用刀片文件中的组件之前添加一个检查,将组件包装在@auth指令之间。就像这样

代码语言:javascript
复制
@auth
    <x-navbar></x-navbar>
@endauth
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69489306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档