0

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
  • 1
    = is for assignment, not for comparison. That's why you always get 1. Use == instead. Commented Dec 5, 2014 at 1:15
  • @Carter Yes thank you I just realized! Commented Dec 5, 2014 at 1:18

2 Answers 2

5

The answer was finally very simple..

Instead of

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

I changed the expression from = (Assignment Operator) to == (Equal)

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

Don't forget to use double equal to compare.

Sign up to request clarification or add additional context in comments.

Comments

3

I could be wrong but I thought you don't need to do comparison with boolean value for a condition check, as it defaults to true.

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

If you want to check for false add the '!'

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

1 Comment

Correct you are

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.