Search code examples
phplaraveleloquentmodellaravel-blade

Access to laravel model vars in a loop


in my laravel project I've a table:

        Schema::create('results', function (Blueprint $table) {
            $table->id();

            $table->tinyInteger('set_1')->nullable();
            $table->tinyInteger('set_2')->nullable();
            $table->tinyInteger('set_3')->nullable();
            $table->tinyInteger('set_4')->nullable();
            $table->tinyInteger('set_5')->nullable();

            $table->timestamps();
        });

which is the right way to show set_* fields in a blade page?

This works but I'm sure there is a better solution

   @php
      $label = ['set_1', 'set_2', 'set_3', 'set_4', '5set_'];
   @endphp
   @for ($i = 0; $i < 5; $i++)
      <tr>
          <td>
{{-- I'm sure there is a better way to do that --}}
             {{ $result[$label[$i]] }}
          </td>
      </tr>
   @endfor


Solution

  • Try this

     // here : $i should be lower or equal 5
     @for ($i = 0; $i <= 5; $i++)
      <tr>
          <td>
             {{ $result["set_$i"] }}
          </td>
      </tr>
     @endfor
    

    i hope it was useful.