Change table color in Laravel Blade

1

Hello. I want to change the background of a table according to the type in DB. I'm using Laravel 5.5. Can you help me?

    
asked by anonymous 30.04.2018 / 05:22

2 answers

0

To resolve, I've done a validation.

    @if( $financa->banco == 1)
      @php $cor = 'red'  @endphp
    @elseif( $financa->banco == 2)
      @php $cor = 'green'  @endphp
    @else
      @php $cor = 'pink'  @endphp
    @endif
    
30.04.2018 / 14:27
0

Firstly, what you should keep in mind is that it is not Laravel that makes the change in specific, but rather a possible class that you set in tr of the table as in the example below:

.mytable tr td, .mytable tr, .mytable {
  padding:0; margin:0; border:none;
}
.mytable tr.red td { background: red; }
.mytable tr.blue td { background: blue; }
.mytable tr.green td { background: green; }
.mytable tr.yellow td { background: yellow; }
.mytable tr.pink td { background: pink; }
<table class="mytable" cellspacing="0" cellpadding="0" border="0">
  <tr><th>nome</th> <th>tipo</th></tr>
  <tr class="red"><td>nome</td> <td>tipo</td></tr>
  <tr class="blue"><td>nome</td> <td>tipo</td></tr>
  <tr class="green"><td>nome</td> <td>tipo</td></tr>
  <tr class="yellow"><td>nome</td> <td>tipo</td></tr>
  <tr class="pink"><td>nome</td> <td>tipo</td></tr>
</table>

So, in order for your problem to be solved you should do something like this:

<!-- forçando uma lista manualmente para o exemplo -->
<?php
    $lista = [
        ['nome' => 'Foo', 'tipo' => 'red'],
        ['nome' => 'Bar', 'tipo' => 'blue']
        ['nome' => 'Bin', 'tipo' => 'green']
        ['nome' => 'Ids', 'tipo' => 'yellow']
        ['nome' => 'Asd', 'tipo' => 'pink']
    ];
?>

<table class="mytable" cellspacing="0" cellpadding="0" border="0">
    <tr>
        <th>nome</th>
        <th>tipo</th>
    </tr>
    @for ($i = 0; $i < count($lista); $i++)
        <tr class="{{ $lista[$i]['tipo'] }}">
            <td>{{ $lista[$i]['nome'] }}</td>
            <td>{{ $lista[$i]['tipo'] }}</td>
        </tr>
    @endfor
</table>
    
30.04.2018 / 17:07