为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
当年话下 2023-12-15 10:43:46
我正在尝试对从控制器返回的值实现条件运算符以创建一些自定义视图。前刀片@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 的一个根本错误。
查看完整描述

4 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

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



查看完整回答
反对 回复 2023-12-15
?
富国沪深

TA贡献1790条经验 获得超9个赞

首先您需要了解何时需要使用大括号。

当您在 Blade 文件中显示数据时,您需要使用大括号。喜欢


Hello, {{ $name }}.

您可以使用@if、@elseif、@else 和@endif 指令构造if 语句。这些指令的功能与其 PHP 对应指令相同:


@if (count($records) === 1)

    I have one record!

@elseif (count($records) > 1)

    I have multiple records!

@else

    I don't have any records!

@endif

您的解决方案


 @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

有关详细信息,请参阅 laravel 文档https://laravel.com/docs/7.x/blade#if-statements


查看完整回答
反对 回复 2023-12-15
?
www说

TA贡献1775条经验 获得超8个赞

您需要删除 if 条件内的 {{ }}。


像这样。


  @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


查看完整回答
反对 回复 2023-12-15
?
慕沐林林

TA贡献2016条经验 获得超9个赞

在控制器中更改您的代码,如下所示 -


$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



查看完整回答
反对 回复 2023-12-15
  • 4 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信