down_load1117 2014-12-05 01:05
浏览 50
已采纳

将布尔值传递给Laravel中的视图

What I try to do:

I try to pass a boolean to my view to to check if it is true or not. If it is set to true, I want to add a class. I use Laravel. Actually, here is my situation:

  • The .red class is added to all rows
  • $finances->depense always return 1 even if it says 0 in the database

Here is my code:

index.blade.php

@foreach($finances as $finance)
    @if ($finance->depense = 1)
        <tr class="red">
    @elseif ($finance->depense = 0) 
        <tr>
    @endif
            <td><a href="{{ URL::to('finances/' . $finance->id) }}">{{$finance->description}}</a></td>
            <td>{{ $finance->depense }}</td>
            <td>{{ $finance->prix }}</td>
            <td>{{ $finance->tps }}</td>
            <td>{{ $finance->tvq }}</td>
            <td>{{ $finance->grandtotal }}</td>
        </tr>
@endforeach

FinancesController.php

public function index()
{
    $finances = Finance::all();
    return View::make('finances.index')->withFinances($finances);
}

What is wrong?

  • 写回答

2条回答 默认 最新

  • douhan9748 2014-12-05 01:17
    关注

    The answer was finaly very simple..

    Instead of

    @if ($finance->depense = 1)
        <tr class="red">
    @elseif ($finance->depense = 0) 
        <tr>
    @endif
    

    I changed the = to ==

    @if ($finance->depense == 1)
        <tr class="red">
    @else 
        <tr>
    @endif
    

    Don't forget to use double equal to compare.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭