首页 > 解决方案 > Laravel Blade 仅在未登录时显示

问题描述

我正在使用 Laravel 的中间件,如果用户未通过身份验证,我会尝试使用空 div。否则,我试图拥有一个允许用户创建帐户的 div。我尝试了以下方法:

@auth
    <div class="form-group col-md-6"></div>
@else
    <div class="form-group col-md-6">
        <h6>Create Account</h6>
    </div>
@endauth

路线

Route::get('/checkout', 'CheckoutController@index')->name('checkout.index')->middleware('auth');
Route::get('/guestCheckout', 'CheckoutController@index')->name('guestCheckout.index');

CheckoutController.php

if (auth()->user() && request()->is('guestCheckout')) {
    return redirect()->route('checkout.index');
}

以上不起作用,我尝试过删除历史记录,清除缓存等,但无济于事。建议?谢谢!

标签: laravellaravel-routinglaravel-middleware

解决方案


如果用户未通过身份验证,则尝试使用空 div ...

您的刀片设置非常接近。尝试这个!

@auth 
 <!-- the user is authenticated --> 
 <div class="form-group col-md-6>
 </div>

@endauth <!-- note the end auth --> 


@guest
 <!-- the user is NOT authenticated --> 
 <div class="form-group col-md-6>
    <h6>Create Account</h6>
 </div>
@endguest

您可以在此处阅读有关此内容的更多信息

至于您的控制器,请尝试使用:

if( Auth::check() ) 
{
    // The user is authenticated.. 

} else {
    // The user is NOT authenticated.. 
}

推荐阅读