向经过验证的用户显示不同的菜单

Displaying different menu to verified users

我怎样才能让它工作:

<ul>
    @guest
    <li><a href="/">Home</a></li>
    <li><a href="#login">Log in</a></li>
    <li><a href="#signup">Sign up</a></li>
    @endguest

    @verified
    <li><a href="/">Dashboard</a></li>
    <li><a href="#profile">Profile</a></li>
    <li><a href="#logout">Log out</a></li>
    @endverified
</ul>

还是坚持使用:

<ul>
    @guest
    <li><a href="/">Home</a></li>
    <li><a href="#login">Log in</a></li>
    <li><a href="#signup">Sign up</a></li>
    @endguest

    @auth
        @if (auth()->user()->email_verified_at)
            <li><a href="/">Dashboard</a></li>
            <li><a href="#profile">Profile</a></li>
            <li><a href="#logout">Log out</a></li>
        @endif
    @endauth
</ul>

这个好像不行:

<ul>
    @guest
    <li><a href="/">Home</a></li>
    <li><a href="#login">Log in</a></li>
    <li><a href="#signup">Sign up</a></li>
    @endguest

    @auth('verified')
    <li><a href="/">Dashboard</a></li>
    <li><a href="#profile">Profile</a></li>
    <li><a href="#logout">Log out</a></li>
    @endauth
</ul>

您需要在 ServiceProvider 类 的 boot 方法之一中添加以下内容。

namespace App\Providers;

use Illuminate\Support\Facades\Blade;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::if('verified', function ($guard = null) {
            return "<?php if(auth($guard)->check() && auth($guard)->user()->email_verified_at): ?>";
        });
    }
}

然后您就可以在 blade 视图中使用它,如下所示

@verified
  user is authenticated and verified
@endverified

Extending Blade - Custom If Statements