Laravel Blade 中带有 {{ }} 的条件运算符的语法

我正在尝试对从控制器返回的值实现条件运算符以创建一些自定义视图。


前刀片


@if({{count($users)}} <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->

          <h3> total number of rows less than or equal to 5 </h3> 

@endif

控制器


$users = User::all();

return view('front', [ 'users'=>$users]);

错误是


语法错误,意外'<'(查看:\resources\views\front.blade.php)


尝试了将条件放在 {{ }} 内或引用运算符或常量值的所有排列组合5错误仍然相同。我是 Laravel 新手,这可能是 Laravel 或 php 的一个根本错误。


当年话下
浏览 114回答 4
4回答

有只小跳蛙

只需删除&nbsp;{{&nbsp;和&nbsp;}},除了 Blade 指令之外,不需要它们(@if在此案例)

富国沪深

首先您需要了解何时需要使用大括号。当您在 Blade 文件中显示数据时,您需要使用大括号。喜欢Hello, {{ $name }}.您可以使用@if、@elseif、@else 和@endif 指令构造if 语句。这些指令的功能与其 PHP 对应指令相同:@if (count($records) === 1)&nbsp; &nbsp; I have one record!@elseif (count($records) > 1)&nbsp; &nbsp; I have multiple records!@else&nbsp; &nbsp; I don't have any records!@endif您的解决方案&nbsp;@if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->&nbsp; &nbsp; &nbsp;<h3> total number of rows less than or equal to 5 </h3>&nbsp;&nbsp; @endif有关详细信息,请参阅 laravel 文档https://laravel.com/docs/7.x/blade#if-statements

www说

您需要删除 if 条件内的 {{ }}。像这样。&nbsp; @if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->&nbsp; &nbsp; &nbsp;<h3> total number of rows less than or equal to 5 </h3>&nbsp;&nbsp; @endif

慕沐林林

在控制器中更改您的代码,如下所示 -$users = User::all();return view('front', compact('users'));刀片文件代码-@if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 --><h3> total number of rows less than or equal to 5 </h3>@endif
打开App,查看更多内容
随时随地看视频慕课网APP