简体   繁体   中英

What is the difference between the below sample codes in Laravel 5.4

What is the difference between the two sample codes below the second returns an error I don't know why

@if ( auth()->check() )
Welcome  {{ Auth::user()->name }}

and this

@if ( {{auth::check() }} )
Welcome  {{ Auth::user()->name }}

Both are in a blade file. Please lets discuss this for better understanding. Also please indicate which is better to use in both scenarios

You shouldn't do this:

@if ( {{auth::check() }} )

It will give you an error. It's almost the same as doing this:

if (echo(auth()->check()))

This code:

{{ auth()->check() }}

Will be converted to this vanilla PHP code:

<?php echo e(auth()->check()); ?>

Where e() is Laravel helper.

https://laravel.com/docs/5.5/blade#displaying-data

They aren't the same, @if ( auth()->check() ) will check if the user is authenticated or not. And the other @if ( {{auth::check() }} ) it's like what @Alexey said, will be converted to <?php echo e(auth()->check()); ?> <?php echo e(auth()->check()); ?> and of course will return an error in your situation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM