亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 263 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號